Reputation:
basically I want to be able to store a 2D array such as this
int [][] preferredMoves = {
{0,0}, {0, arrLength}, {length%2, arrLength},
{0, length%2}, {arrLength, 0}, {0, length%2},
{arrLength, arrLength}, {length%2, length%2},
{arrLength, length%2}, {length%2, 0}
};
In a single
int [] moves;
array.
I'm sure this is possible since I'm just storing a list..., but I can't seem to find information on this anywhere... or maybe its not possible?
EDIT I am dealing with matrices. I want to store the list in a single array to then return that array to use it elsewhere.
So then every time I call it, all I have to do is something like this...
int row = Computer.moves()[0];
int col = Computer.moves()[1];
I also need to loop through that single array, which contains the 2D array multiple times..
Upvotes: 0
Views: 55
Reputation: 558
Janos' answer is probably what you want. Alternatively you could create a class, e.g. Pair
, and store it in a Pair[]
array.
Upvotes: 0
Reputation: 124704
Not sure if this is what you meant,
but you could drop the internal { ... }
to convert this to a one-dimensional array:
int [] moves = {
0, 0, 0, arrLength, length % 2, arrLength,
0, length % 2, arrLength, 0, 0, length % 2,
arrLength, arrLength, length % 2, length % 2,
arrLength, length % 2, length % 2, 0
};
And you can translate 2D indexes (i, j)
to 1D index k
using the formula:
k = i * 2 + j;
Upvotes: 3