I'm a little stuck on the arithmetic for this program. I have a string theString
that has a length of x*y*z
. x
, y
, and z
are the dimensions of a char[][][] array (lets call it cArr
).
I want to insert the characters of theString
into cArr
based on a specific index. Here's an example of a 5x4x3 array.
String theString = someMethod(); //returns a string of length 60
char[][][] cArr = new char[5][4][3];
for (int z = 0; z < 3; z++) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y][z] = theString.charAt(/*~~~*/);
}
}
}
I can figure out the arithmetic to insert characters into a char[5][4] array from a string of length 20:
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y] = theString.charAt((5*y)+x);
}
}
What arithmetic should I be using in the charAt()
method for a 3-dimensional char array?
My best attempt has been:
char[][][] cArr = new char[5][4][3];
for (int z = 0; z < 3; z++) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y][z] = theString.charAt(((3^2)*z)+(4*y)+x);
}
}
}
But this makes all z-index layers of cArr
the same.