在使用for循环打印2D数组中不同行和列的值。

huangapple go评论86阅读模式
英文:

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

问题

我有一个二维数组第一行是名字第二行是姓氏

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

期望的输出是像这样打印出来:

Phil Garcia
Kim Gimena
等等...

现在,我通过捕捉 ArrayIndexOutOfBoundsException 异常来实现了这个功能,输出如下:

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

这很好,但我想知道是否有一种方法可以在不捕捉异常的情况下实现这一点?我一直在尝试让代码更简洁,但没有成功。

目前的代码如下:

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("");
    }
}

正如你所看到的,我在内部循环中对 i 变量使用了 +1,这导致了异常。有什么建议吗?我目前想保持这种使用两个循环的方式,因为我对Java还很陌生。


<details>
<summary>英文:</summary>

I have this 2d array of the first row being first names and the second being last names. 

```java
String[][] ladrones2 = {
        {&quot;Phil&quot;, &quot;Kim&quot;, &quot;Phil&quot;, &quot;Perry&quot;},
        {&quot;Garcia&quot;, &quot;Gimena&quot;, &quot;Basinger&quot;, &quot;Ornitorrinco&quot;}};

the prefered out come of this would be to have them printend out like e.g

Phill Garcia
Kim Gimena
etc...

Now I was able to do this by catching the ArrayIndexOutOfBoundsException exception and it works and the output is:

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 = {
            {&quot;Phil&quot;, &quot;Kim&quot;, &quot;Phil&quot;, &quot;Perry&quot;},
            {&quot;Garcia&quot;, &quot;Gimena&quot;, &quot;Basinger&quot;, &quot;Ornitorrinco&quot;}};

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

as you can see I used in the inner for loop a +1 on the i variable which in turn throws the exception.

Any ideas? I would like just to keep this way of working at the moment with 2 forloops due to being new to Java.

答案1

得分: 1

如果你确定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]);
    }
}
英文:

If you are sure that ladrones2 array always has 2 rows, then below code would be simpler:

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

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

答案2

得分: 1

在这行代码中:

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

你同时从第一个和第二个数组中取出一个元素,但在这行循环中,你正在遍历序列中的所有元素:

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

要纠正这个问题,你只需要将 i 的范围减小到 ladrones.length-1,否则你将会达到数组的末尾并尝试访问下一个不存在的元素。

最终你应该使用这行代码:

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

On this line :

System.out.println(&quot;name: &quot; + ladrones2[i][j] + &quot; &quot; + 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 &lt; 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.

You'll end up with this line instead :

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

huangapple
  • 本文由 发表于 2020年5月30日 23:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/62104604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定