简体   繁体   中英

List of 2D Arrays return last array

I have a problem.

List<Color[][]> list = new ArrayList<Color[][]>();
for (int i = 0; i < 5; i++) {           
    if (i == 0) tab[1][1] = Color.white;
    if (i == 1) tab[1][1] = Color.blue;
    if (i == 2) tab[1][1] = Color.black;
    if (i == 3) tab[1][1] = Color.red;
    if (i == 4) tab[1][1] = Color.cyan;
    System.out.println(i + " add " + tab[1][1]);
    list.add(tab);  
}
System.out.println("_");
for (int i = 0; i < 5; i++)
    System.out.println(i + ". read " + list.get(i)[1][1]);

And output is:

0 add java.awt.Color[r=255,g=255,b=255]
1 add java.awt.Color[r=0,g=0,b=255]
2 add java.awt.Color[r=0,g=0,b=0]
3 add java.awt.Color[r=255,g=0,b=0]
4 add java.awt.Color[r=0,g=255,b=255]

0. read java.awt.Color[r=0,g=255,b=255]
1. read java.awt.Color[r=0,g=255,b=255]
2. read java.awt.Color[r=0,g=255,b=255]
3. read java.awt.Color[r=0,g=255,b=255]
4. read java.awt.Color[r=0,g=255,b=255]

Why list return only last array?

You only have one instance of tab and store 5 references to it. Then you change tab , and the change is reflected everywhere it is referenced.

In other words, System.out.println(i+" add "+tab[1][1]); prints the state of tab at each addition, which is different after each loop iteration, System.out.println(i+". read "+list.get(i)[1][1]); prints the state of after the last addition, which does not change between iterations of the respective loop.

Here you are changing value for the same reference, hence last value is getting overided. so create new object for Color[][] inside for loop.

Because you are always adding elements at 1,1 and adding same tab 5 times. Instead i think you need:

for(int i=0;i<5;i++){           
    if(i==0) tab[i][i] = Color.white;
    if(i==1) tab[i][i] = Color.blue;
    if(i==2) tab[i][i] = Color.black;
    if(i==3) tab[i][i] = Color.red;
    if(i==4) tab[i][i] = Color.cyan;
    System.out.println(i+" add "+tab[i][i]);
}
list.add(tab);  //add just once

And print like:

for(int i=0;i<5;i++)
    System.out.println(i+". read "+list.get(0)[i][i]);//using dynamic index

You should read up the basic of java. First off you are using the same index of the array in the loop instead of iterating. This is how i think you mean to write it.

List<Color[][]> list = new ArrayList<Color[][]>();
for(int i=0;i<5;i++){           
    if(i==0) tab[i][i] = Color.white;
    if(i==1) tab[i][i] = Color.blue;
    if(i==2) tab[i][i] = Color.black;
    if(i==3) tab[i][i] = Color.red;
    if(i==4) tab[i][i] = Color.cyan;
    System.out.println(i+" add "+tab[i][i]);
    list.add(tab);  
}
System.out.println("_");
for(int i=0;i<5;i++)
    System.out.println(i+". read "+list.get(i)[i][i]);

Also there is no need in this code to use multi dimensional array nor a list of it.

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