无法从 Main 内的嵌套循环更新 Main 中的数组。我如何分配它的价值?
Cannot update an array in Main from nested loops inside of Main. How can I assign it's value?
所以,我在编写程序时突然 运行 遇到一个问题,我的三个数组打印为 null、0 和 null。
程序从文件中读取值,并根据迭代次数将它们分配给三个数组。
这是我的代码:
String mushroom []= new String [10];
int weight [] = new int [10];
String cooking[] = new String [10];
FileReader fr = new FileReader ("7J_Mushrooms.csv");
BufferedReader br = new BufferedReader (fr);
System.out.println("Mushroom\t\tWeight\t\tCooking\n=======================================================");
String line = "";
while ((line = br.readLine()) != null){
String [] temp = line.split(",");
for (int i = 0; i < temp.length; i++){
if(i == 0){
mushroom[i] = temp[i];
}
else if (i == 1){
weight[i] = Integer.parseInt(temp[i]);
}
else{
cooking[i] = temp[i];
}
}
}
// This bit just sorts them by weight in ascending order like a parallel array
for (int i = 0; i < weight.length-1; i++){
for (int j = 0; j < weight.length-1;j++){
if (weight[j] > weight[j+1]){
int temp = weight [j];
String tempMush = mushroom [j];
String tempCook = cooking [j];
weight[j] = weight[j+1];
mushroom[j] = mushroom[j+1];
cooking[j] = cooking[j+1];
weight[j+1] = temp;
mushroom[j+1] = tempMush;
cooking[j+1] = tempCook;
}
}
}
for (int i = 0; i < weight.length; i++){
System.out.print(mushroom[i] + "\t\t" + weight[i] + "\t\t" + cooking[i] + "\n");
}
当我在 for 循环内打印数组的值时,这些值是正确的,但是在 while 循环外,代码打印为 null、null 和 0。但是,打印了最后三个值,但是我确定这与我的问题有关。
无论如何,我认为这与范围有关。
经过一番查找,我发现 java 是 "Pass by Value" 而不是 "Pass by Reference"。我不太了解这个原则,但据我所知,它特别影响方法,但我的所有代码都在一个方法下——main。我尝试在 for 循环内部和外部使用 return,但它也不起作用!
您最初读取值的方式似乎很不对:您将相应的 weight
和 cooking
放入与实际 mushroom
不同的索引中,并且您正在使用index i
以完全错误的方式。应该是
int i = 0;
while ((line = br.readLine()) != null){
String[] temp = line.split(",");
if(temp.length == 3)
mushroom[i] = temp[0];
weight[i] = Integer.parseInt(temp[1]);
cooking[i] = temp[2];
} else {
// some error handling
}
i++;
}
"When I print the values of the arrays inside of the for loop, the values are correct" 是错误的 - 如果您检查 temp
值是正确的,但 mushroom
、weight
和 cooking
永远不会用您的代码正确填充。
进一步说明:
- 尝试使用自定义 class 来保存关联的值,而不是处理 3 个数组,这些数组 神奇地 相互关联。然后对 class
的实例数组进行排序
你是writing/over-writing每个蘑菇中的前三个元素,烹饪和重量。
作为
i < temp.length, i <= 3
所以,我在编写程序时突然 运行 遇到一个问题,我的三个数组打印为 null、0 和 null。
程序从文件中读取值,并根据迭代次数将它们分配给三个数组。
这是我的代码:
String mushroom []= new String [10];
int weight [] = new int [10];
String cooking[] = new String [10];
FileReader fr = new FileReader ("7J_Mushrooms.csv");
BufferedReader br = new BufferedReader (fr);
System.out.println("Mushroom\t\tWeight\t\tCooking\n=======================================================");
String line = "";
while ((line = br.readLine()) != null){
String [] temp = line.split(",");
for (int i = 0; i < temp.length; i++){
if(i == 0){
mushroom[i] = temp[i];
}
else if (i == 1){
weight[i] = Integer.parseInt(temp[i]);
}
else{
cooking[i] = temp[i];
}
}
}
// This bit just sorts them by weight in ascending order like a parallel array
for (int i = 0; i < weight.length-1; i++){
for (int j = 0; j < weight.length-1;j++){
if (weight[j] > weight[j+1]){
int temp = weight [j];
String tempMush = mushroom [j];
String tempCook = cooking [j];
weight[j] = weight[j+1];
mushroom[j] = mushroom[j+1];
cooking[j] = cooking[j+1];
weight[j+1] = temp;
mushroom[j+1] = tempMush;
cooking[j+1] = tempCook;
}
}
}
for (int i = 0; i < weight.length; i++){
System.out.print(mushroom[i] + "\t\t" + weight[i] + "\t\t" + cooking[i] + "\n");
}
当我在 for 循环内打印数组的值时,这些值是正确的,但是在 while 循环外,代码打印为 null、null 和 0。但是,打印了最后三个值,但是我确定这与我的问题有关。
无论如何,我认为这与范围有关。
经过一番查找,我发现 java 是 "Pass by Value" 而不是 "Pass by Reference"。我不太了解这个原则,但据我所知,它特别影响方法,但我的所有代码都在一个方法下——main。我尝试在 for 循环内部和外部使用 return,但它也不起作用!
您最初读取值的方式似乎很不对:您将相应的 weight
和 cooking
放入与实际 mushroom
不同的索引中,并且您正在使用index i
以完全错误的方式。应该是
int i = 0;
while ((line = br.readLine()) != null){
String[] temp = line.split(",");
if(temp.length == 3)
mushroom[i] = temp[0];
weight[i] = Integer.parseInt(temp[1]);
cooking[i] = temp[2];
} else {
// some error handling
}
i++;
}
"When I print the values of the arrays inside of the for loop, the values are correct" 是错误的 - 如果您检查 temp
值是正确的,但 mushroom
、weight
和 cooking
永远不会用您的代码正确填充。
进一步说明:
- 尝试使用自定义 class 来保存关联的值,而不是处理 3 个数组,这些数组 神奇地 相互关联。然后对 class 的实例数组进行排序
你是writing/over-writing每个蘑菇中的前三个元素,烹饪和重量。 作为
i < temp.length, i <= 3