So, I am trying to iterate all the elements in a 2D array using a single loop.
Here where I am:
public class Test {
private static final String[][] key = {
{"`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace"},
{"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"},
{"Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter"},
{"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "\u2191"},
{" ", "<", "\u2193", ">"}
};
public static void main(String[] args) {
final int totalLen = 57;
String str = "";
for (int i = 0, row = 0, col = 0; i < totalLen; ++i, ++col) {
if (row < key.length && i % key[row].length >= key[row].length - 1) {
++row;
col = 0;
System.out.println(str);
str = "";
}
if (row < key.length)
str += col + " ";
}
}
}
I commented on the range of indices for each row that the above program should output, but this is not the way the logic is faulty. Any suggestions?
Edit: The loop condition should remain unchanged.