如何检查双数组中的索引?

时间:2014-08-16 14:32:19

标签: java android arrays if-statement for-loop

int[][] mytime = {
        {10, 33},  
        {11, 23},  
        {9, 13},  
        {16, 53},
};

for (int h=1; h < mytime[0]; h++)  // checking for the first index (ie: 10, 11, 9, 16)
for (int m=1; h < mytime[1]; h++)  // checking for the second index (ie: 33, 23, 14, 53)

如何进行for循环直到其中一个索引检查?

4 个答案:

答案 0 :(得分:0)

我不确定one of the index checks到底是什么意思,但是这里有你如何得到第一列和第二列的数字:

for (int i = 0; i < mytime.length; i++) {
    int first = mytime[i][0];
    int second = mytime[i][1];
}

您必须迭代从0mytime.length-1的行。

答案 1 :(得分:0)

你会做点什么

int[][] mytime = {
        {10, 33},  
        {11, 23},  
        {9, 13},  
        {16, 53}
};

int[] firstIndex = new int[mytime.length];
int[] secondIndex = new int[mytime.length];
for (int h = 0; h < mytime.length; h++) {
    firstIndex[h] = mytime[h][0];
    secondIndex[h] = mytime[h][1];
}

答案 2 :(得分:0)

您的帖子未显示您尝试的内容以及您获得的结果/失败。这将有助于人们更好地了解您的问题。

public class TotoTo {
    public void foo() {
        int[][] mytime = { { 10, 33 }, { 11, 23 }, { 9, 13 }, { 16, 53 }, };

        for (int h = 0; h < mytime.length; h++) {
            System.out.println(mytime[h][0]);
        }
        for (int h = 0; h < mytime.length; h++) {
            System.out.println(mytime[h][1]);
        }
    }

    public static void main(String... params) {
        new TotoTo().foo();
    }
}

请注意,在Java中(与许多其他编程语言一样,数组索引从0 开始,而不是从1开始。

此外,您必须小心使用这种结构:如果在某个时间点mytime数组的内容发生变化,该怎么办?你可以获得一个ArrayOutOfBoundException ...

int[][] mytime = { { 10 }, { 11, 23 }, { 9, 13 }, { 16, 53 }, };

答案 3 :(得分:0)

我假设您正在尝试打印您指定的所有值。在这种情况下,以下是代码:

int[][] mytime={{10, 33},  
        {11, 23},  
        {9, 13},  
        {16, 53},
    };
    for(int h=0;h<4;h++)//for the rows i.e 10,11,9,16
    {
        for(int m=0;m<2;m++)//for the columns i.e 33,23,13,53
        {
        System.out.println(mytime[h][m]);
        }
    }

希望这有帮助。