问题描述
什么是做Java中的可变数组的最佳方式?我试着用向量,但在何时,当你做一个插入由移动的所有元素,我需要一个可以成长的数组,但元素留在原地。我敢肯定有这一个简单的答案,但是我还是不太清楚。
What is the best way to do a resizable array in Java? I tried using Vector, but that shifts all elements over by when when you do an insert, and I need an array that can grow but the elements stay in place. I'm sure there's a simple answer for this, but I still not quite sure.
推荐答案
作为替代方案,您可以使用一个的。这是List接口的一个可变数组的实现。
As an alternative, you could use an ArrayList. It is a resizable-array implementation of the List interface.
使用(使用字符串):
List<String> myList = new ArrayList<String>();
myList.add("a");
myList.add("c");
myList.add("b");
此次订货会像你一样把它们放在:A,C,B
The order will be just like you put them in: a, c, b.
您还可以得到一个单独的项目是这样的:
You can also get an individual item like this:
String myString = myList.get(0);
这将给你的第0个元素:A
Which will give you the 0th element: "a".
这篇关于如何使Java中的可变数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!