Data structure to store 2D-arrays in JAVA

14,146

Solution 1

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

Solution 2

An array is not just an idea about how to store information, it is also an implementation of how to store data. Thus, if you use an array, you have already selected your data structure.

If you want to store data in a data structure, you need to concentrate on how the data structure is used, think about how you will retrieve data and store data, how often you do each operation, and how much data you will be working with. Then you know which methods must be optimum, and have an idea of whether the data can reside in memory, etc.

Just to give you an example of how many ways this could be solved:

  1. You could flatten the array into a 1D array, and use x*num_columns+y as the index
  2. You could create an Object to contain the pair, and put the array in a Map
  3. You could use a linked list containing linked lists.
  4. You could use a tree containing trees.
  5. You could use a list containing trees.
  6. You could create a partial order over the pair and then put all the elements into one tree.

All of these solutions depend heavily on which operations are more important to optimize. Sometime it is more important to update the data structure quickly, sometimes not. The deciding factor is really the rest of your program.

Solution 3

So you want to store a collection of 2D arrays: if the collection is fixed size add another dimension:

int[][][] arrColl

If the collection is variably sized, use your favorite implementation of Collection<int[][]> (ArrayList, LinkedList, etc.):

Collection<int[][]> arrColl
Share:
14,146
Christos Baziotis
Author by

Christos Baziotis

Updated on June 09, 2022

Comments

  • Christos Baziotis
    Christos Baziotis almost 2 years

    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.