java Java中使用ArrayList的二维动态数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6232257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
2D dynamic array using ArrayList in Java
提问by user288609
I need to implement a 2D dynamic array. The number of rows is fixed, say n. But the number of columns for each row is not fixed and equivalent. For instance, the first row has 3 elements and the second row has 5 elements. How to do this in Java using Arraylist. Thanks.
我需要实现一个二维动态数组。行数是固定的,比如 n。但是每行的列数不是固定的和等效的。例如,第一行有 3 个元素,第二行有 5 个元素。如何使用 Arraylist 在 Java 中执行此操作。谢谢。
采纳答案by Jigar Joshi
How about List<List<Foo>>
?
怎么样List<List<Foo>>
?
For Example:
例如:
List<List<Foo>> list = new ArrayList<List<Foo>>();
List<Foo> row1 = new ArrayList<Foo>();
row1.add(new Foo());
row1.add(new Foo());
row1.add(new Foo());
list.add(row1);
List<Foo> row2 = new ArrayList<Foo>();
row2.add(new Foo());
row2.add(new Foo());
list.add(row2);
回答by MarioP
ArrayList<ArrayList<SomeObject>> twodlist = new ArrayList<ArrayList<SomeObject>>();
ArrayList<SomeObject> row = new ArrayList<SomeObject>();
row.add(new SomeObject(/* whatever */));
// etc
twodlist.add(row);
row = new ArrayList<SomeObject>();
// etc
回答by x4u
You can either use a array for the rows since this dimenstion is fixed:
您可以为行使用数组,因为此维度是固定的:
@SuppressWarnings("unchecked")
ArrayList<T>[] arr = new ArrayList[ fixedsize];
or use nested ArrayLists:
或使用嵌套的 ArrayLists:
List<List<T>> list = new ArrayList<List<T>>( fixedsize );
回答by Ted Hopp
Try:
尝试:
ArrayList<ArrayList<DataType>> array = new ArrayList<ArrayList<DataType>>();
for (int i = 0; i < n; ++i) {
array.add(new ArrayList<DataType>());
}
回答by CodeRedd
As you say, you can make an array of arraylists and use the ArrayList(int initial capacity) constructor to set the capacity of each column:
正如您所说,您可以创建一个数组列表并使用 ArrayList(int initial capacity) 构造函数来设置每列的容量:
ArrayList<YourObject>[] rows=new ArrayList<YourObjects>[n];
for(i=0;i<n;i++){
rows[i]=ArrayList<YourObjects>(initialsize);
}
回答by Matt Razza
You could create an array of ArrayList elements because your row count is fixed.
您可以创建一个 ArrayList 元素数组,因为您的行数是固定的。
ArrayList[] dynamicArray = new ArrayList[n]();
Note: You'll need to allocate an ArrayList object in each entry in the array. So...
注意:您需要在数组的每个条目中分配一个 ArrayList 对象。所以...
for (int loop = 0; loop < n; loop++)
dynamicArray[loop] = new ArrayList();
OR if you'd like both rows and columns to be dynamic you could create an ArrayList of ArrayLists....
或者,如果您希望行和列都是动态的,您可以创建一个 ArrayLists 的 ArrayList ....
ArrayList<ArrayList<T>> dynamicArray = new ArrayList<ArrayList<T>>();
Once again, you'll need to create an array list in each new entry to dynamicArray.
再一次,您需要在 dynamicArray 的每个新条目中创建一个数组列表。
回答by erickzetta
if the number of rows is fixed, try something like this:
如果行数是固定的,请尝试以下操作:
ArrayList<MyObject>[] = new ArrayList<MyObject>[fixedRows]
回答by trutheality
List<ArrayList<SomeObject>> twoDList = new ArrayList<List<SomeObject>>(n);
for( int i=0; i<n; i++ )
twoDList.add( new ArrayList<SomeObject>() );
Use as:
用于:
twoDList.get(rownumber).add(newElementInColumn);
回答by Doug
I would create an array of ArrayList (ArrayList[3] rows = new ArrayList[3] if the rows were 3) Then for each row create column classes and insert them into an ArrayList. and then place the ArrayList into the Array. the row array's index can be used to keep track of the row number. Remember arrays start there indexes at 0 so the row number would be rows[index+1]
我会创建一个 ArrayList 数组(ArrayList[3] rows = new ArrayList[3] 如果行是 3)然后为每一行创建列类并将它们插入到一个 ArrayList 中。然后将 ArrayList 放入 Array。行数组的索引可用于跟踪行号。记住数组从 0 处开始索引,所以行号将是 rows[index+1]