为什么此代码的输出是
island = Fiji
island = Cozumel
island = Bermuda
island = Azores
为什么输出从斐济岛而不是“百慕大”开始?百慕大的array中有0个元素。您能否指出我为什么我的输出有这样的特定顺序。
public class TestArrays {
public static void main (String[] args){
int y = 0;
int[] index = new int [4];
index[0] = 1;
index[1] = 3;
index[2] = 0;
index[3] = 2;
String[] islands = new String[4];
islands[0] = "Bermuda";
islands[1] = "Fiji";
islands[2] = "Azores";
islands[3] = "Cozumel";
int ref;
while ( y < 4){
ref = index[y];
System.out.print("island = ");
System.out.println(islands[ref]);
y = y + 1;
}
}
最佳答案
您的索引顺序为{1、3、0、2}
int[] index = new int [4];
index[0] = 1;
index[1] = 3;
index[2] = 0;
index[3] = 2;
因此它将打印岛[1],岛[3],岛[0],岛[2]
String[] islands = new String[4];
islands[0] = "Bermuda";
islands[1] = "Fiji";
islands[2] = "Azores";
islands[3] = "Cozumel";
斐济,科苏梅尔,百慕大,亚速尔群岛
关于java - 数组输出结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39561371/