简体   繁体   中英

ArrayList of type 2D array in Java

I have declared an ArrayList along the lines of this at the beginning of my code:

ArrayList<int[][]> arraylists = new ArrayList<>();

Since Netbeans is not giving me an error, I am assuming that syntax wise, this is a valid declaration.

I added a 2D array element like this:

int[][] newarray = {{0},{0}};
arraylists.add(newarray);

I called upon the ArrayList like so:

int[][] array = arraylists.get(0);

Since I have already added something to the ArrayList, I will not get an index out of bounds exception.

However, it is not getting the 2D Array at index 0. To debug I tried doing this:

System.out.println("testing");
int[][] array = arraylists.get(0);

The output is this: testing

However, when I tried doing this:

int[][] array = arraylists.get(0);
System.out.println("testing");

The output is nothing

I think this means that there is a problem with getting the 2D array within the ArrayList as no other error pops up. I think it would be good to mention that nothing else after getting the 2D array from the ArrayList works. So the program stop there. I am just beginning my code so this is just about all I have so far, and I plan on continuing the code once this problem is resolved. Note that Netbeans is not alerting me saying that there is anything wrong. It just isn't working...

Can anyone help me to identify what is wrong?

Probably just Netbeans being silly. Try cleaning the project and rebuild. This code works just fine:

public static void main(String[] args) {
    List<int[][]> list = new ArrayList<>();
    list.add(new int[][]{{1,2}, {3,4}});
    int[][] twoD = list.get(0);
    System.out.println(Arrays.deepToString(twoD));
}

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