下面是主要方法:
public static void main(String[] args) throws IOException{
int [] array = new int [1000];
int[] getArray=FileArrayProvider(array);
for (int y = 0; y < getArray.length; ++y)
{
System.out.print(array[y] + " ");
}
我想将“数组”传递给下面的“FileArrayProvider”方法:
public static int[] FileArrayProvider(int[] array1) throws IOException{
File file = new File("RandomNumbers.txt");
FileReader inputFile = new FileReader(file);
BufferedReader in = new BufferedReader(inputFile);
{String s =in.readLine();
System.out.println("Array before sorting: ");
while(s!=null)
{int k = 0;
array1[k] = Integer.parseInt(s);
System.out.print(array1[k]+" ");
s = in.readLine();
}
in.close();
}
return array1;
}
问题是数组没有被传回主方法。它在“FileArrayprovider”方法中正确打印,但仅将数组的最后一个值传递回 main 方法,并用除此之外的所有零填充数组。任何提示或建议将不胜感激来解决这个问题!谢谢