简体   繁体   English

在 Java 中使用 ArrayList 的二维动态数组

[英]2D dynamic array using ArrayList in Java

I need to implement a 2D dynamic array.我需要实现一个二维动态数组。 The number of rows is fixed, say n.行数是固定的,比如 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.例如,第一行有 3 个元素,第二行有 5 个元素。 How to do this in Java using Arraylist.如何使用 Arraylist 在 Java 中执行此操作。 Thanks.谢谢。

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);
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

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 );

Try:尝试:

ArrayList<ArrayList<DataType>> array = new ArrayList<ArrayList<DataType>>();
for (int i = 0; i < n; ++i) {
    array.add(new ArrayList<DataType>());
}

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);
}

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.注意:您需要在数组的每个条目中分配一个 ArrayList object。 So...所以...

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....或者,如果您希望行和列都是动态的,您可以创建一个 ArrayList 的 ArrayLists...。

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 的每个新条目中创建一个数组列表。

if the number of rows is fixed, try something like this:如果行数是固定的,请尝试以下操作:

ArrayList<MyObject>[] = new ArrayList<MyObject>[fixedRows]
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);

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.我将创建一个 ArrayList 数组(ArrayList[3] rows = new ArrayList[3] 如果行是 3)然后为每一行创建列类并将它们插入 ArrayList。 and then place the ArrayList into the Array.然后将 ArrayList 放入阵列中。 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]记住 arrays 从 0 开始索引,所以行号是 rows[index+1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM