简体   繁体   中英

Java 2D arraylists

I cant understand 2D arraylists, they are confusing me, I can understand 2D arrays however as I worked with them before in C and in Python as "nested lists" can someone explain the difference between these 2 codes?

ArrayList<ArrayList<String>> biDemArrList = new ArrayList<ArrayList<String>>();
ArrayList<String> temp = new ArrayList<String>(); // added ()
temp.add("Hello world.");
temp.add("sup");
biDemArrList.add(temp);
ArrayList<String> it = new ArrayList<String>();
it.add("1");
it.add("0");
biDemArrList.add(it);
System.out.println(temp);
System.out.println(it);
System.out.println(biDemArrList);

and this one :

ArrayList[][] table = new ArrayList[10][10];
table[0][5] = new ArrayList();
table[1][1] = new ArrayList();
for (int i = 0; i < 12; i++) {
    table[0][5].add("0");
}
for (int i = 0; i < 9; i++) {
    table[1][1].add("1");
}
System.out.println(table[0][5]);
System.out.println(table[9][9]);

Like in C arrays of non primitive types are not initialized (only arrays of primitive types are...).

 ArrayList[][] table = new ArrayList[10][10]; table[0][5] = new ArrayList(); table[1][1] = new ArrayList(); 

Here you create an array of 100 elements but you only initialize 2 Elements.

ArrayList is resizable-array implementation of the List interface. This class. Most of the developers choose Arraylist over Array as it's a very good alternative of traditional java arrays.

You can add any object to List, eg null , String , Object , String[] . ArrayList<String> also is object, it's means you can add to list.

You said I have ArrayList which can add other ArrayList . The result will be ArrayList<ArrayList>> . But we want to add only String's to inner ArrayList. And we create ArrayList<String>

So, We have list of string ArrayList<String> which can be added to other list ArrayList<ArrayList>>

    ArrayList<ArrayList<String>> mainArrayList = new ArrayList<ArrayList<String>>();
    ArrayList<String> subArrayList = new ArrayList<String>();

    /* Added elements into subArrayList */
    subArrayList.add("Yogesh");
    subArrayList.add("Pawar");

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

    /* Added elements into subArrayList2    */
    subArrayList2.add("Java");
    subArrayList2.add("Programmer");

    /* Adding elements into mainArrayList */
    mainArrayList.add(subArrayList);
    mainArrayList.add(subArrayList2);
    for (int i = 0; i < mainArrayList.size(); i++) {
        for (int k = 0; k < mainArrayList.get(i).size(); k++) {
            System.out.print(" " + mainArrayList.get(i).get(k));
        }
        System.out.println();
    }

The difference between

List of List

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

and

Array of Array of List

ArrayList[][] table = new ArrayList[10][10];

Is that the second one is not actually two-dimensional, it is three-dimensional. You end up with 10 Arrays of length 10 that you can put ArrayLists into. Where as in the List of List example you have a List you can put other Lists into.

Using the Object[][] or primitive[][] you have to allocate the 2D array with exact number of "rows" and "columns" like new Object[2][8] .

On the other hand with ArrayList<ArrayList<...>> try to understand the following code:

 ArrayList<ArrayList<String>> biDemArrList = new ArrayList<>();
    ArrayList<String> a0 = new ArrayList<>();
    a0.add("string_1");
    ArrayList<String> a1 = new ArrayList<>();
    a1.add("strfdfas");
    a1.add("adfadsfasdfasdfasfaf");

    biDemArrList.add(a0);
    biDemArrList.add(a1);

    biDemArrList.stream().forEach(System.out::println);

The first "row" has one element, and the second one has two elements. This is only an example... With arr[][] you cannot achieve this.

What is reason behind this not sure, But i would share my experience here,

Array is the fixed size of data structure, once we initialize the array we can't modify the size. To resolve this we have ArrayList comes to picture. Arraylist has variable lenght.

In your second code snippet, if you are looking for fixed sized of 2D ArrayList, I would suggest to go 2D Arrays.

If you want to get benefit of Collection features, later you can convert Arrays to ArrayList object.

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