我是java的新手,并且是为了这个而做的。几个小时以来,我一直在反对它。我最初只使用多个ArrayLists来做这个,但我的教授希望我使用一个ArrayList重做它,我无法弄明白。头 - >壁。
最终目标是我的数组列表包含由我的自定义类组成的对象。 我试图从文件生成ArrayList。 我试图使用自定义类来生成ArrayList中的对象。
我相信我已经缩小了我未正确将文件正确加载到自定义类中的问题。
请注意我在哪里" Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the file name: ");
fileName = keyboard.next();
//file name: genWeatherSort.txt
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
ArrayList<Weather_Class> list = new ArrayList<Weather_Class>();
while (inputFile.hasNext()){
int day = inputFile.nextInt();
double highTemp = inputFile.nextDouble();
double recHighTemp = inputFile.nextDouble();
int year = inputFile.nextInt();
double humid = inputFile.nextDouble();
Weather_Class weather = new Weather_Class(day, highTemp, recHighTemp,
year, humid);
System.out.print(weather);
list.add(weather);
}
inputFile.close();
&#34;,以下打印件:
Weather_Class @ 55f96302Weather_Class @ 3d4eac69Weather_Class @ 42a57993Weather_Class @ 75b84c92Weather_Class @ 6bc7c054Weather_Class @ 232204a1Weather_Class @ 4aa298b7
似乎每个都代表一个对象,这是有道理的,因为文件中会有7个对象,但我希望天气可以打印7次,每行打印1个,以及打印的是什么out,也应该添加到ArrayList&#34; list&#34;作为一个对象。
public class Weather_Class {
private int day;
private double highTemp;
private double recHighTemp;
private int year;
private double humid;
public Weather_Class(int day, double highTemp,
double recHighTemp, int year, double humid){
this.day = day;
this.highTemp = highTemp;
this.recHighTemp = recHighTemp;
this.year = year;
this.humid = humid;
}
public int getDay(){
return day;
}
public double getHighTemp(){
return highTemp;
}
public double getRecHighTemp() {
return recHighTemp;
}
public int getYear() {
return year;
}
public double getHumid() {
return humid;
}
}
Weather_Class:
15 88.98 89.2 1944 1.7
104 63.83 78.3 1981 25.8
179 80.89 117.3 2008 39.0
202 83.45 93.4 1947 39.5
262 57.82 125.3 1983 52.0
364 120.61 124.8 1985 27.0
0 0.00 0.0 0 0.0
以下是我使用的txt文件中的信息:
geom_path