I have a large array of arbitrary size. It's a square array. I'm trying to grasp how to traverse it diagonally like a /
rather than a \
(what I already know how to do). I have the following code thus far:
char[][] array = new char[500][500];
//array full of random letters
String arrayLine = "";
for (int y = 0; y < array.length; y++) {
for (int x = 0; x < array.length; x++) {
for (???) {
arrayLine = arrayLine + array[???][???];
}
}
System.out.println(arrayLine);
}
I have three loops, because this is how I did the other diagonal:
for (int y = 0; y < array.length; y++) {
for (int x = 0; x < array.length; x++) {
for (int z = 0; z < array.length-y-x; z++) {
arrayLine = arrayLine + array[y+z][x+z];
}
}
System.out.println(arrayLine);
}
In my attempts, I keep going outside the boundaries and get an ElementOutOfBounds exception. Say the array is as below (a 3x3 instead of 500x500):
A B C
D E F
G H I
I want to print out the following as strings:
A
BD
CEG
FH
I
A previous SO question had a similar problem with integer arrays, and the solution is based off of the sum of array elements. But I'm working with chars, so I can't think of a methodology to get it.
Think about the coordinates of the cells:
. 0 1 2
0 A B C
1 D E F
2 G H I
For any diagonal, all of the elements have something in common: the sum of an element's coordinates is a constant. Here are the constants:
0 = 0+0 (A)
1 = 1+0 (B) = 0+1 (D)
2 = 2+0 (C) = 1+1 (E) = 0+2 (G)
3 = 2+1 (F) = 1+2 (H)
4 = 2+2 (I)
The minimum constant is the smallest coordinate sum, 0. The maximum constant is the largest coordinate sum. Since each coordinate component can go up to array.length - 1
, the maximum constant is 2 * (array.length - 1)
.
So the thing to do is iterate over the constants. For each constant, iterate over the elements whose coordinates sum to the constant. This is probably the simplest approach:
for (int k = 0; k <= 2 * (array.length - 1); ++k) {
for (int y = 0; y < array.length; ++y) {
int x = k - y;
if (x < 0 || x >= array.length) {
// Coordinates are out of bounds; skip.
} else {
System.out.print(array[y][x]);
}
}
System.out.println();
}
However, that will end up iterating over a lot of out-of-bounds coordinates, because it always iterates over all possible y
coordinates, even though only one diagonal contains all possible y
coordinates. Let's change the y
loop so it only visits the y
coordinates needed for the current k
.
One condition for out-of-bounds coordinates is x < 0
. Substitute the definition of x
and solve:
x < 0
k - y < 0
k < y
y > k
So when y > k
, x
will be negative. Thus we only want to loop while y <= k
.
The other condition for out-of-bounds coordinates is x >= array.length
. Solve:
x >= array.length
k - y >= array.length
k - array.length >= y
y <= k - array.length
So when y <= k - array.length
, x
will be too large. Thus we want to start y
at 0 or k - array.length + 1
, whichever is larger.
for (int k = 0; k <= 2 * (array.length - 1); ++k) {
int yMin = Math.max(0, k - array.length + 1);
int yMax = Math.min(array.length - 1, k);
for (int y = yMin; y <= yMax; ++y) {
int x = k - y;
System.out.print(array[y][x]);
}
System.out.println();
}
Note: I have only proven this code correct. I have not tested it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With