如果有人能向我解释如何可以得到这两个阵列彼此相邻,将不胜感激打印。如何打印两个增强的for循环彼此相邻,以
package labs;
import java.util.Arrays;
public class ArrayOfObjects {
public static void main (String[] args){
String[] pets = {"dog", "cow", "sheep", "cat", "horse"};
String[] names = {"Spot", "Milky", "Bahhhhd", "Meooooow", "Nayyybor"};
for (String name : names){
System.out.println(name.toString());
}
for (String type : pets){
System.out.println(type.toString());
}
}
}
输出我得到的是明显
Spot
Milky
Bahhhhd
Meooooow
Nayyybor
dog
cow
sheep
cat
horse
然而
....我想
Spot dog
Milky cow
Bahhhhd sheep
Meooooow cat
Nayyybor horse
有一件事我没有尝试是改变两个增强的for循环和使用他们内部的toString方法并没有让它打印数组,但是当我将两个数组输出到不同的for循环中时,我在增强for循环中使用的变量未被初始化。
不能并行运行两个如你所愿。你必须使用两个'迭代器' –
问题不在于增强for循环,而是在使用并行数组的代码设计中。这是你为什么要避免使用并行数组而是创建一个同时包含两个字符串的类的又一个原因。 –
谢谢大家我明白了,我只是想学习和探索增强型for循环,因为我是初学者。 – Conner