简体   繁体   English

2d ArrayList放置

[英]2d ArrayList placement

I want to use a ArrayList instead of an array on the following code: 我想在以下代码上使用ArrayList而不是数组:

for(int k=0;k<SIZE;k++) //size is 9
    for(int j=0;j<SIZE;j++)
        ar1[k][j] = buttons[k][j].getText();

That's how the ArrayList should look like I guess: 这就是ArrayList应该看起来像我的样子:

ArrayList<ArrayList<String>>ar1 = new ArrayList<ArrayList<String>>();

But it's so confusing since I can't use the get method .. I don't know how to do that. 但这很令人困惑,因为我无法使用get方法..我不知道该怎么做。

Just try this: ar1.get(k).get(j) 只需尝试: ar1.get(k).get(j)

The first get() gets the ArrayList at index k ; 第一个get()在索引k处获取ArrayList; the second get() gets the String you want. 第二个get()获取所需的字符串。

If you want to set the String at [k, j] , you can use ar1.get(k).set(j, str) . 如果要将String设置为[k, j] ,则可以使用ar1.get(k).set(j, str) Of course, ar1.get(k) can't be null. 当然, ar1.get(k)不能为null。 You'd have to add or set an ArrayList with something like ar1.add(new ArrayList<String>()); 您必须使用诸如ar1.add(new ArrayList<String>());类的东西添加或设置一个ArrayList ar1.add(new ArrayList<String>()); the first time you use the ArrayList at that index. 第一次在该索引处使用ArrayList。

Similarly, you can use ar1.get(k).add(str) to add str to the end of the ArrayList at index k if that ArrayList has too few elements. 同样,如果ArrayList的元素太少,则可以使用ar1.get(k).add(str)将str添加到ArrayList的索引k处。

Try this way 试试这个

List<List<String>>ar1 = new ArrayList<>();
//lets say we want to have array [2, 4]
//we will initialize it with nulls
for (int i=0; i<2; i++){
    ar1.add(new ArrayList<String>());
    for(int j=0; j<4; j++)
        ar1.get(i).add(null);
}
System.out.println("empty array="+ar1);

//lets edit contend of that collection
ar1.get(0).set(1, "position (0 , 1)");
ar1.get(1).set(3, "position (1 , 3)");
System.out.println("edited array="+ar1);

//to get element [0, 1] we can use: ar1.get(0).get(1)
System.out.println("element at [0,1]="+ar1.get(0).get(1));

Output: 输出:

empty array=[[null, null, null, null], [null, null, null, null]]
edited array=[[null, position (0 , 1), null, null], [null, null, null, position (1 , 3)]]
element at [0,1]=position (0 , 1)

Its always a good practice to use List type when declaring ArrayList . 在声明ArrayList时使用List类型始终是一个好习惯。 So instead of 所以代替

ArrayList<ArrayList<String>>ar1 = new ArrayList<ArrayList<String>>();

use 使用

List<List<String>>ar1 = new ArrayList<List<String>>();

You can easily add elements in the arraylist through .add(element) method. 您可以通过.add(element)方法轻松地在arraylist中添加元素。 Read the ever helpful javadoc for the complete list of list methods. 阅读有帮助的javadoc,以获得完整的列表方法列表。 At first of course it is empty. 首先当然是空的。 To start populating it, you need to create a 1d arraylist 要开始填充它,您需要创建一个一维数组列表

List<String> a = new ArrayList<String>()
a.add("element");

Then add the 1d arraylist in you 2d arraylist 然后在您的2d arraylist中添加1d arraylist

ar1.add(a);

To insert an element in a 2d list, you must access the position of the 1d arraylist where you want to insert then do an .add(String) . 要将元素插入2d列表中,必须访问1d arraylist要插入的位置,然后执行.add(String)

ar1.get(0).add("newly added element"); //will get the first list then append the string to that list

Same as, to get an element in the 2d list 与在2d列表中获取元素相同

ar1.get(0).get(0); //will get the first lis then get the first element of the pulled list

A 2-d array is probably a better abstraction, but if you do want to make an ArrayList of ArrayLists for other reasons, it can be done. 二维数组可能是更好的抽象方法,但是如果您出于其他原因想要创建ArrayLists的ArrayList,则可以这样做。 Just remember that you need to add the row itself before you add an element to the row. 只需记住,在将元素添加到行之前,需要先添加行本身。

//Initialize the rows
ArrayList<ArrayList<String>> ar1 = new ArrayList<ArrayList<String>>
for (int i=0; i<SIZE; i++) {
    ar1.add(new ArrayList<String>());
    for (int j=0; j<SIZE; j++) {
        ar1.get(i).set(j, "");
    }
}

//Now setting a value works
ar1.get(row).set(column, value);

//As does getting one
String s = ar1.get(row).get(column);

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

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