1

I have created a 2D Array as follows

String names [][] = new String [5][2];

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

And the OutPut:

Jack  | 2013
Peter | 2012
Cham  | 2011
Jayce | 2013
Robin | 2011
Jax   | 2013

I'm required to print values containing 2013 in this array . Please help.

4

2 回答 2

1

您可以在循环中比较字符串names[i][0].equals("2013")

for (int i = 0; i < names.length; i++) {
            if (names[i][0].equals("2013")) {
                System.out.print(names[i][1] + "   ");
                System.out.println(names[i][0]);
            }
        }

输出

jack   2013
Jayce  2013
Jax    2013
于 2013-09-03T05:53:18.943 回答
1

如果您只想打印包含“2013”​​的行,则需要在循环中添加 if

for(int i=0; i<names.length;i++){
    if(names[i][0].equals("2013") {
        System.out.println(names[i][1]);
        System.out.println(names[i][0]);
    }
}
于 2013-09-03T05:54:12.220 回答