Data structure to store 2D-arrays in JAVA
arraylist, arrays, java, multidimensional-array
Solution
If you need to store a number of `int[][]` arrays in a data structure, I would probably recommend that you store the `int[][]` arrays in an `Object` that represents what the data contains, then store these `Objects` in an `ArrayList`.
For example, here is a simple `Object` wrapper for your `int[][]` arrays
public class 2DArray {
int[][] array;
public 2DArray(int[][] initialArray){
array = initialArray;
}
}
And here is how you would use them, and store them in an `ArrayList`
// create the list
ArrayList<2DArray> myList = new ArrayList<2DArray>();
// add the 2D arrays to the list
myList.add(new 2DArray(myArray1));
myList.add(new 2DArray(myArray2));
myList.add(new 2DArray(myArray3));
The reason for my suggestion is that your `int[][]` array must have some meaning to you. By storing this in an `Object` wrapper class, you can give it a meaning. For example, if the values were co-ordinates, you would call your class `Coordinates` instead of `2DArray`. You, therefore, create a `List` of `Coordinates`, which has a lot more meaning than `int[][][]`.
Problem
i am looking for a data structure to store two dimensional integer arrays. Is List the rigth data structure or should i use another one? Can someone give me a short example on how to create such a data structure and how to add a 2d array? Edit: I want a data structure in which i want to store int[11][7] arrays. For instance ten, int[11][7] arrays.