正如標題所示,我試圖從一個名爲Hill的類調用下面的方法到我的公共類,我也想將返回列表存儲在本地列表中。在另一個類中調用方法並存儲返回的列表
public static List<Hill> readHills() {
String destination = "****";
List<Hill> hill = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader(new File(destination)));
String fileContents = "";
int line = 0;
while ((fileContents = br.readLine()) != null) {
if (line == 0) {
line++;
continue;
} else {
String[] entries = fileContents.split(",");
Hill placeholder = new Hill(Integer.parseInt(entries[0]), entries[1], entries[2], Double.parseDouble(entries[4]), Double.parseDouble(entries[5]), Double.parseDouble(entries[6]));
hill.add(placeholder);
line++;
}
}
}
catch(FileNotFoundException fnfe) {
System.out.println("Unable to find file");
}
catch(IOException e) {
System.out.println("Error");
}
return hill;
}
這正是我想要但援引上述
public static void exercise5b() {
Hill mylist = new Hill();
//List<Integer> mylist = Hill.readHills();
}
的方法,該方法這樣做時,我得到一個錯誤在山();說Hill不能在Hill()中應用。下面還代碼顯示了我的課希爾包含
class Hill {
public int number;
public String name;
public String county;
public double height;
public double latitude;
public double longitude;
Hill (int number, String name, String county, double height, double latitude, double longitude){
this.number = number;
this.name = name;
this.county = county;
this.height = height;
this.latitude = latitude;
this.longitude = longitude;
}
您已經重寫了默認構造函數,因此在構造對象時必須提供「Hill」構造函數的參數。或者,您可以創建一個沒有任何參數的構造函數,然後它就會有效。 –