繁体   English   中英

从1D获取2D数组索引

[英]Get 2D array indices from 1D

所以,我试图使用一个循环遍历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", "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 + " ";
        }
    }
}

我已经评论了上面程序应该输出的每一行的索引范围,但它没有因为逻辑错误。 有什么建议?

编辑:循环条件必须保持不变。

您应该能够使用两个变量rowcol来执行此操作,其中col在循环标头中递增,并且row循环体中有条件地递增:

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并在达到totalLen时停止:

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;
    }
}

但是,这更加脆弱,因为您正确计算了totalLen的依赖性。

尝试这个解决方案,它使用单个while循环和问题中的相同循环条件(根据要求):

String str = "";
int row = 0, col = 0;
int i = 0, totalLen = 57;

while (i < totalLen) {
    if (col < key[row].length) {
        str += col++ + " ";
        i++;
    } else {
        System.out.println(str);
        str = "";
        row++;
        col = 0;
    }
}
System.out.println(str); // prints the last line

或者,如果您更喜欢使用for循环:

String str = "";
int totalLen = 57;

for (int i = 0, row = 0, col = 0; i < totalLen; i++) {
    str += col++ + " ";
    if (col == key[row].length) {
        row++;
        col = 0;
        System.out.println(str);
        str = "";
    }
}

代码段将在控制台上生成以下输出:

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;
        }
    }
}

基本上它与@dasblinkenlight几乎相同 - 只有当row变量达到key数组的长度时才引入break条件。 您还可以测试if(i==totalLen)但我个人觉得检查数组长度更可靠。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM