gpt4 book ai didi

java - 数组输出结构

转载 作者:行者123 更新时间:2023-12-01 09:29:31 25 4
gpt4 key购买 nike

为什么这段代码的输出是

island = Fiji
island = Cozumel
island = Bermuda
island = Azores

为什么输出从 Fiji island 开始,而不是“Bermuda”?百慕大数组中有 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;

}

}

最佳答案

很简单:

ref = index[y];

以上行将 ref 输出为 1,因为当 index[y] 结果为 1 y0

因此,

ref = 1  //index[0] = 1;

现在,下面一行:

System.out.println(islands[ref]);

输出:

island = Fiji

因为ref1,而islands[ref]代表斐济,如下所示:

islands[1] = "Fiji";

关于java - 数组输出结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39561371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com