Store 2D array in array?

So, I have this method. This creates a 2D array.

public static int[][] createCacheArray(int cacheSize, int blockSize, int[] memory, int i) {

    //Create multi-dimension array for cache.

    int [][] cache = new int [cacheSize/blockSize][blockSize];

    for (int column = 0; column < blockSize; column++) {
      for (int row = 0; row < cache.length; row++) {
           cache[row][column] = memory[i];
           i++;
      } 
    }

    return cache;

}

      

I would like to save the generated Cache 2D array to a 1D array because I will be creating multiple 2D cache arrays using the same method and I would like to organize them.

Basically I want to go to the [number] array where the Cache 2D array is located, and then read the contents of that 2D cache array. If there is another method I would gladly appreciate.

Another way to do it:

cache = createCacheArray(cacheSize, blockSize, memory, (cacheSize * 0));
cache1 = createCacheArray(cacheSize, blockSize, memory, (cacheSize * 1));
cache2 = createCacheArray(cacheSize, blockSize, memory, (cacheSize * 2));
cache3 = createCacheArray(cacheSize, blockSize, memory, (cacheSize * 3));
      

Run codeHide result


Here cache is a 2D array, cache1 is a 2D array, cache 2 is a 2D array, and so on, but this is inconvenient. The reason they are requested is because they are fixed 64x16 2D arrays. If the array gets full then I have to make another 2D array with the same size. Instead of creating multiple variables, I thought it was possible to store them in an array somehow. Like 2D arrays are books and 1st array is a shelf.

==================

2D arrays have numbers inside.

After executing AndersonVieira's suggestion, all my 2D arrays are stored in an ArrayList. It's amazing.

All I want to do is search for a specific number in all these 2D arrays inside the ArrayList. But I don't know what code to write for this. I am still new to Java.

+3


source to share


2 answers


You can create List<int[][]>

and store your caches there:

List<int[][]> caches = new ArrayList<>();
for (int i = 0; i < numCaches; i++) {
    caches.add(createCacheArray(cacheSize, blockSize, memory, (cacheSize * i)));
}

      

Then you can access the caches by following these steps:

int[][] cache = caches.get(j);

      

Where j

is the index of the desired item in the list caches

.

Or, if you need to do something for each saved cache, you can iterate through the list:

for (int[][] cache : caches) {
    doSomething(cache);
}

      



This will probably be easier to work with than using a 3D array.

Edit:

To check if a 2-dimensional array contains an element, you need to go through all positions, comparing the position value with the desired value:

static boolean contains(int element, int[][] cache) {
    for (int i = 0; i < cache.length; i++) {
        for (j = 0; j < cache[i].length; j++) {
            if (cache[i][j] == element) {
                return true;
            }
        }
    }
    return false;
}

      

Then you can create a method that returns the first cache containing the specified item, or null

else:

static int[][] cacheContaining(int element, List<int[][]> caches) {
    for (int[][] cache : caches) {
        if (contains(element, cache)) {
            return cache;
        }
    }
    return null;
}

      

+2


source


You can use a 3D array like



int[][][] cache = {
    createCacheArray(cacheSize, blockSize, memory, (cacheSize * 0)),
    createCacheArray(cacheSize, blockSize, memory, (cacheSize * 1)),
    createCacheArray(cacheSize, blockSize, memory, (cacheSize * 2)),
    createCacheArray(cacheSize, blockSize, memory, (cacheSize * 3))
};
System.out.println(Arrays.deepToString(cache);

      

+1


source







All Articles