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