Reputation: 667
For the simplicity of the questions let's imagine we were storing a :
int[][][][] values = new int[10][10][10][10];
I'm currently looking to generate tables of values which will be available for looking up later by the computer (not by a person). These values are basically scores for situations. So for example if we wanted to see the score where the first component is 2 the second is 3 the third is 7 and the fourth is 9 then we could go to [2][3][7][9] in the file instead of having to compute the value on the fly.
I understand the technical details of writing and reading to a file but I'm having a hard time getting my head around how to do this.
Let's say with a 2D array and I wanted to find the value of [X][Y] then I could just read and write to the row with value X and the column with value Y.
However, with a 3 or even 4 D array how can we store those values in a text file so that we don't have to generate the array everytime we run the program?
Upvotes: 2
Views: 753
Reputation: 5476
The easiest way to save this to a file would be using Serializable interface which is already implemented in primitive types (according to this post)
Example:
int[][][][] data = new int[10][10][10][10];
// Write object to a file
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("serializedArray.data")));
oos.writeObject(data);
oos.flush();
oos.close();
// Read an object from a file
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("serializedArray.data")));
Object obj = ois.readObject();
ois.close();
// Cast it back to an int array
data = (int[][][][])obj;
Upvotes: 1
Reputation: 3
Java is an object oriented language based on Classes. IMHO you should work with it the way its made for. Either make an Object with 4 fields or a quadratic diamond class:
class Quad<A, B, C, D>
I repeat again this is my honest opinion, mass downvotes INC.
Upvotes: 0