简体   繁体   中英

2d ArrayList placement

I want to use a ArrayList instead of an array on the following code:

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<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.

Just try this: ar1.get(k).get(j)

The first get() gets the ArrayList at index k ; the second get() gets the String you want.

If you want to set the String at [k, j] , you can use ar1.get(k).set(j, str) . Of course, ar1.get(k) can't be null. You'd have to add or set an ArrayList with something like ar1.add(new ArrayList<String>()); the first time you use the ArrayList at that index.

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.

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 . 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. Read the ever helpful javadoc for the complete list of list methods. 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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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