简体   繁体   English

使用for循环打印出二维数组中不同行和列的值

[英]Printing out values of different rows and column in 2d array using for loops

I have this 2d array of the first row being first names and the second being last names.我有这个第一行的二维数组是名字,第二行是姓氏。

String[][] ladrones2 = {
        {"Phil", "Kim", "Phil", "Perry"},
        {"Garcia", "Gimena", "Basinger", "Ornitorrinco"}};

the prefered out come of this would be to have them printend out like eg最好的结果是让它们打印出来,例如

Phill Garcia
Kim Gimena
etc...

Now I was able to do this by catching the ArrayIndexOutOfBoundsException exception and it works and the output is:现在我能够通过捕获 ArrayIndexOutOfBoundsException 异常来做到这一点,并且它可以工作,并且 output 是:

name Phil Garcia
name Kim Gimena
name Phil Basinger
name Perry Ornitorrinco

Which is great but I wondered if there was a way to do this without having to catch the exception?这很好,但我想知道是否有办法做到这一点而不必捕获异常? I've been searching to make this cleaner but wasn't able to do so.我一直在寻找使这个更清洁的方法,但没能做到。

Code at the moment:目前的代码:

public static void robo2() {
    String[][] ladrones2 = {
            {"Phil", "Kim", "Phil", "Perry"},
            {"Garcia", "Gimena", "Basinger", "Ornitorrinco"}};

    try {
        for (int i = 0; i < ladrones2.length; i++) {
            for (int j = 0; j < ladrones2[i].length; j++) {
                System.out.println("name: " + ladrones2[i][j] + " " + ladrones2[i + 1][j]);
            }
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("");
    }
}

as you can see I used in the inner for loop a +1 on the i variable which in turn throws the exception.正如你所看到的,我在内部 for 循环中使用了i变量的+1 ,这反过来又引发了异常。

Any ideas?有任何想法吗? I would like just to keep this way of working at the moment with 2 forloops due to being new to Java.由于是 Java 的新手,我现在只想使用 2 个 forloops 保持这种工作方式。

If you are sure that ladrones2 array always has 2 rows, then below code would be simpler:如果您确定ladrones2数组总是有 2 行,那么下面的代码会更简单:

public static void main(String[] args) {
    String[][] ladrones2 = {
            {"Phil", "Kim", "Phil", "Perry"},
            {"Garcia", "Gimena", "Basinger", "Ornitorrinco"}};

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

On this line:在这条线上:

System.out.println("name: " + ladrones2[i][j] + " " + ladrones2[i + 1][j]);

You are taking an element from the first and second array at the same time but you are looping over all the elements in the sequence on this line:您同时从第一个和第二个数组中获取一个元素,但是您正在循环此行中序列中的所有元素:

for (int i = 0; i < ladrones2.length; i++) 

To correct this problem, you simply have to reduce the range of i to ladrones.length-1 , otherwise you will reach the end of the array and try to access the next element which doesn't exist here.要纠正这个问题,您只需将i的范围缩小到ladrones.length-1 ,否则您将到达数组的末尾并尝试访问此处不存在的下一个元素。

You'll end up with this line instead:你最终会得到这一行:

for (int i = 0; i < ladrones2.length-1; i++) {

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

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