3

So I have this method here. This is creating 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 store the created Cache 2D array into a 1D array because I will be making multiple 2D cache arrays using this same method and I would like to organize them.

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

The other way of doing it is this:

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));

Here, cache is 2D array, cache1 is a 2D array, cache 2 is a 2D array and so on, but this is inconvenient. The reason why im asking is because these are fixed 64x16 2D arrays. If an array gets filled, then I have to make another 2D array with the same size. Instead of making multiple variables, I was thinking it was possible to somehow store these onto an array. Like the 2D arrays are books and the 1D array is the shelf.

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

The 2D arrays have numbers inside them.

After doing AndersonVieira's suggestion, all of my 2D arrays are stored into the ArrayList. This is perfect.

All I want to do now is search for a specific number in all of those 2D arrays inside the ArrayList. But I don't know what code to write to do this. I'm still a beginner in Java.

4
  • How would you want to add it to a 1D array ? What would be a given 2D array and it expected 1D array ? Commented Apr 20, 2015 at 0:30
  • 1
    Have you considered using a 3d array? Or a Map? Commented Apr 20, 2015 at 0:36
  • I updated my post. Basically the method makes a 2D array. But the only way I know to store each 2D array is by making multiple variables. I was thinking if it is possible to organize this in some way. Commented Apr 20, 2015 at 0:37
  • @ElliottFrisch The 2D arrays that i'm making are fixed. I can only make a 64x16 2D array, so after this is filled up with numbers, I need to make another 2D 64x16 arrays and so on until all the numbers are placed into the arrays. Commented Apr 20, 2015 at 0:43

2 Answers 2

2

You could create a List<int[][]> and store your caches in there:

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

Then, you could access the caches by doing:

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

Where j is the index of the desired element in the caches list.

Or, if you need to do something to each stored cache, you could iterate on the list:

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

This would 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 loop through all the positions comparing the position value to 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 could create a method that returns the first cache that contains the specified element, or null otherwise:

static int[][] cacheContaining(int element, List<int[][]> caches) {
    for (int[][] cache : caches) {
        if (contains(element, cache)) {
            return cache;
        }
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Is there a way to do this without adding "cache1", "cache2"..etc? I would like to do this in a for loop.
for(int i = 0; i < numCaches; i++) { caches.add(createCacheArray(...)); }
This method is perfect, just one more question. Once the list has been created, there is a number that I need to find from all of these 2D array list created. How would I do this now that the 2D arrays are organized into an arraylist?
@user3311682 Do you need to search each cache to see if they contain a certain number? Assuming you have a function that does this to a single cache, you can iterate on the list and apply this function to each stored cache. I updated the answer with an example using a method called doSomething().
@AndersonVieira I kind of need help. Using the example above, it stored all of the 2D arrays to the ArrayList. Suppose that I want to find a number and its stored in of the 2D arrays. Im guessing a for loop can help but I'm not sure how to find a specific value. I just need this part and I can mark this as solved.
|
1

You could 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.