Java 从1D中获取2D数组索引
所以,我尝试使用一个循环遍历2D数组中的所有元素 我就在这里:Java 从1D中获取2D数组索引,java,arrays,loops,Java,Arrays,Loops,所以,我尝试使用一个循环遍历2D数组中的所有元素 我就在这里: public class Test { private static final String[][] key = { {"`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace"}, // 0 - 13 {"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I",
public class Test {
private static final String[][] key = {
{"`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace"}, // 0 - 13
{"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"}, // 0 - 13
{"Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter"}, // 0 - 12
{"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "\u2191"}, // 0 - 11
{" ", "<", "\u2193", ">"} // 0 - 3
};
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 + " ";
}
}
}
公共类测试{
私有静态最终字符串[][]密钥={
{“`”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“0”,“-”,“=”,“Backspace”},//0-13
{“Tab”,“Q”,“W”,“E”,“R”,“T”,“Y”,“U”,“I”,“O”,“P”,“[”,“]”,“\\”},//0-13
{“Caps”、“A”、“S”、“D”、“F”、“G”、“H”、“J”、“K”、“L”、“;”、“'”、“Enter”},//0-12
{“Shift”、“Z”、“X”、“C”、“V”、“B”、“N”、“M”、“M”、“s”、“s”、“s”、“/”、“\u2191”},//0-11
{" ", ""} // 0 - 3
};
公共静态void main(字符串[]args){
最终整合素=57;
字符串str=“”;
for(int i=0,row=0,col=0;i=key[row].length-1){
++行;
col=0;
系统输出打印项次(str);
str=“”;
}
if(行<键长度)
str+=col+“”;
}
}
}
我已经对上面程序应该输出的每一行的索引范围进行了注释,但是没有注释,因为逻辑是错误的。有什么建议吗
编辑:循环条件必须保持不变。您应该能够使用两个变量
行
和列
,其中列
在循环头中递增,而行
在循环体中有条件递增:
for (int row =0, col = 0 ; row != key.length ; col++) {
System.out.println(row + " " + col);
if (col == key[row].length-1) {
row++;
col = 0;
}
}
循环条件不能不同。对不起,我忘了提那件事
您也可以添加i
并在到达totalen
时停止:
for (int row =0, col = 0, i = 0 ; i != totalLen ; col++, i++) {
System.out.println(row + " " + col);
if (col == key[row].length-1) {
row++;
col = 0;
}
}
但是,这更脆弱,因为您需要依赖于正确计算的
totalen
。尝试此解决方案,它使用单个while
循环和问题中相同的循环条件(根据要求):
任一代码段都将在控制台上生成以下输出:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 1 2 3 4 5 6 7 8 9 10 11 12
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3
好的,我试着保持你的循环状态不变。。。但我并没有利用每一个参数。无论如何,它应该是有效的:
for (int i = 0, row = 0, col = 0; i < totalLen; ++i, ++col) {
System.out.println("row " + row + ", col " + col);
// what ever you want to do with your loop, do it here
// before altering the row and col parameters
if (col == key[row].length-1) {
col = -1; // if end of the row, set col to -1, it will be incremented in the next loop
row++;
if(row==key.length){
break;
}
}
}
for(int i=0,row=0,col=0;i
基本上,它与@dasblinkenlight所做的几乎相同–只是当
行
变量达到键
数组的长度时,我引入了一个中断
条件。您也可以测试if(i==totalen)
,但我个人认为检查数组长度更可靠。使用“幻数”(57)和坚持“条件必须保持不变”有什么意义?可读性差,可维护性差。。。如果元素的数量不是57,会发生什么?期待一个例外!?我在对其中一个答案的评论中解释了原因57
不是一个幻数,它是数组中元素的总数i
被用作另一个与此处无关的数组的索引。@laune Edit:“如果元素数不是57,会发生什么?预期会出现异常!?”首先,您需要冷静一下。元素的数量在前面计算过,所以不可能不是57。我从示例中去掉了不相关的细节。我只是惊讶于OP坚持支持一个非自然循环条件,将final(totalen)设置为一个值“计算得更早”,即在编译时之前。为什么假设它在编译时之前?在这个方法中使用之前的“更早”。我写了一个循环来计数它,然后存储它——运行时。我不需要把这个循环包括进去,把问题弄得一团糟。我只是对你对这个条件的执着感到惊讶。两种解决方案都打印了不正确的输出,OP的代码打算为每一行生成col
值,请参见我的答案。@user2570380,你是对的,我更正了它。很抱歉
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 1 2 3 4 5 6 7 8 9 10 11 12
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3
for (int i = 0, row = 0, col = 0; i < totalLen; ++i, ++col) {
System.out.println("row " + row + ", col " + col);
// what ever you want to do with your loop, do it here
// before altering the row and col parameters
if (col == key[row].length-1) {
col = -1; // if end of the row, set col to -1, it will be incremented in the next loop
row++;
if(row==key.length){
break;
}
}
}