【发布时间】:2019-01-24 09:14:03
【问题描述】:
我正在做一项学校作业,我需要使用我编写的一些方法从 ArrayList 访问数据。
public static void main(String[] args) throws IOException {
String fileName = "USPopulation.txt";
File fileReader = new File(fileName);
ArrayList<Integer> populations = new ArrayList<Integer>();
Scanner inputFile = new Scanner(fileReader);
while (inputFile.hasNext()) {
populations.add(inputFile.nextInt());
}
System.out.println(populations.greatest());
/**
* Receives an ArrayList and returns the index of the value that has the
* greatest increase from the previous value in the collection.
*
* @param populations
* @return greatestI
*/
public static int greatest(ArrayList<Integer> populations) {
int greatestDiff = 0;
int greatestI = 0;
int i = 0;
while (i < populations.size() - 1) {
int tempDiff = populations.get(i + 1) - populations.get(i);
if (tempDiff >= greatestDiff) {
greatestDiff = tempDiff;
greatestI = i + 1;
}
i++;
}
return greatestI;
}
}
当我尝试调用我的方法时,最棒的是,我遇到了错误
对于 ArrayList 类型,未定义方法 best()。
我以为我需要的定义包含在我的方法的参数中,但显然没有。
错误消息和我发现的故障排除使我似乎需要将我的 ArrayList populations 转换为方法知道如何处理的类型,但我尝试的任何方法似乎都不起作用。
感谢任何帮助。感谢任何花时间帮助菜鸟的人。 返回最小的I;
【问题讨论】:
-
您似乎在努力学习 Java 语言基础知识。请重新访问任何体面的教程。这不是你调用方法的方式。
标签: java arraylist methods casting