英文:
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 = {
{"Phil", "Kim", "Phil", "Perry"},
{"Garcia", "Gimena", "Basinger", "Ornitorrinco"}};
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 = {
{"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.
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 = {
{"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]);
}
}
答案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("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.
You'll end up with this line instead :
for (int i = 0; i < ladrones2.length-1; i++) {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论