读取文件的方式有问题。代码似乎希望一行包含整个数字列表。然而
Scanner
Find
最后,文件中的每个数字都会被调用一次。
Scanner scanner = new Scanner(file).useDelimiter(",");
这意味着不需要
mapToInt
Collections
例如
ArrayList
,并在转换为整数后向其追加数字。
public static void main(String[] args) throws FileNotFoundException {
@SuppressWarnings("resource")
File file = new File(args [0]);
Scanner scanner = new Scanner(file).useDelimiter(",");
try {
ArrayList<Integer> Ray = new ArrayList<>();
while (scanner.hasNext()) {
String num = scanner.next().trim();
Ray.add(Integer.parseInt(num));
}
scanner.close();
int[] arr = Ray.stream().mapToInt(i -> i).toArray();
Find(arr, 0, arr.length - 1);
} catch (NumberFormatException ex) {
}
}
阵列列表
与具有固定大小的阵列不同,它允许根据需要进行扩展。这很方便,因为这意味着您不必提前知道文件中的整数数量。
保持与您的
函数,我将
阵列列表
返回正常阵列。但是,我建议您更新函数以接受
.