如何增加数组大小并在数组顶部添加每个新元素? [英] How to increase array size and add every new element on the top of the array?
问题描述
您知道如何使用新元素动态增加数组的大小,而每个新元素都位于Java中数组的顶部吗?
Do you know how to increase dynamically the size of the array with new elements and every new element goes on the top of the array in Java?
我有以下Java代码段:
I am having this Java snippet:
List<String> dbsuggestions = new ArrayList<String>();
...some for loop here
String correctedText = correctMedicalAcronym(ac, classify, c);
if(correctedText != null){
System.out.println(correctedText);
dbsuggestions.add(0, correctedText);
}
...end of the for loop
推荐答案
您知道如何使用新的
元素动态增加数组的大小,并且每个新元素都位于Java数组的顶部吗?
Do you know how to increase dynamically the size of the array with new elements and every new element goes on the top of the array in Java?
您可以使用 List
,它的所有实现都会增加根据要求动态调整大小。如果您希望每个新元素都位于顶部并在移除时也始终从顶部弹出,我建议您通过堆栈
You can use List
, all of it's implementations will take care of increasing the size dynamically as per requirements. If you want every new element to go on top and while removing, you also pop always from top, I will recommend, you go through Stack
这是 Stack $ c的合同$ c>将每个元素放在顶部,并从顶部删除每个元素。例如:
It's the contract of Stack
to put every element on top and remove every element from top. Forexample:
Stack<String> stackDemo = new Stack<>();
stackDemo.push("First");
stackDemo.push("Second");
stackDemo.push("Third");
System.out.println(stackDemo.pop()); // will return Third
这篇关于如何增加数组大小并在数组顶部添加每个新元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!