Java 无法从Main中的嵌套循环更新Main中的数组。我如何分配它';s值多少?
所以,我在编写程序时,突然遇到了一个问题,我的三个数组打印为null、0和null 程序从文件中读取值,并根据迭代次数将它们分配给三个数组 这是我的密码:Java 无法从Main中的嵌套循环更新Main中的数组。我如何分配它';s值多少?,java,arrays,nested-loops,Java,Arrays,Nested Loops,所以,我在编写程序时,突然遇到了一个问题,我的三个数组打印为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"); BufferedRead
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");
}
String蘑菇[]=新字符串[10];
整数权重[]=新整数[10];
串烹饪[]=新串[10];
FileReader fr=新的FileReader(“7J_蘑菇.csv”);
BufferedReader br=新的BufferedReader(fr);
System.out.println(“蘑菇状\t\tweeght\t\t烹饪\n=================================================================================================================================================================================================================================================================================================================================;
字符串行=”;
而((line=br.readLine())!=null){
String[]temp=line.split(“,”);
对于(int i=0;i重量[j+1]){
内部温度=重量[j];
字符串tempMush=蘑菇[j];
字符串tempCook=cooking[j];
重量[j]=重量[j+1];
蘑菇[j]=蘑菇[j+1];
烹饪[j]=烹饪[j+1];
重量[j+1]=温度;
蘑菇[j+1]=tempMush;
烹饪[j+1]=临时烹饪;
}
}
}
对于(int i=0;i
当我在for循环内部打印数组的值时,值是正确的,但是在while循环外部,代码被打印为null、null和0。然而,最后三个值已打印出来,但我确信这与我的问题有关
无论如何,我相信这与范围有关
经过一些搜索,我发现java是“按值传递”而不是“按引用传递”。我并不真正理解这个原理,但据我所知,它特别影响方法,但我所有的代码都在一个方法main下。我尝试在for循环内部和外部使用return,但它也不起作用 您最初在中读取值的方式似乎很不正确:您将相应的
重量
和烹饪
放入与实际的蘑菇
不同的索引中,并且您使用的索引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++;
}
“当我打印for循环内部数组的值时,值是正确的”是错误的-如果您检查temp
值是正确的,但是muscle
,weight
和cooking
从未正确填充代码
进一步说明:
- 尝试使用自定义类来保存关联的值,而不是处理3个数组,它们神奇地相互关联。然后对该类的实例数组进行排序
重量
和烹饪
放在与实际的蘑菇
不同的索引中,并且您以完全错误的方式使用索引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++;
}
“当我打印for循环内部数组的值时,值是正确的”是错误的-如果您检查temp
值是正确的,但是muscle
,weight
和cooking
从未正确填充代码
进一步说明:
- 尝试使用自定义类来保存关联的值,而不是处理3个数组,它们神奇地相互关联。然后对该类的实例数组进行排序
i
iwhile
循环中设置一个断点,并在每次迭代中检查三个数组,以查看实际发生的情况there@Samuelf80你能在ideone.com上发布你的代码并分享链接吗,这样会更容易找到正确的答案problem@Samuelf80最后一件事可能是,null
值在第一个位置被排序,导致您在数组中的实际值之前看到很多null