在Java中创建多个数据类型的数组列表

我正在使用的一些文件: http : //pastebin.com/WriQcuPs

目前我不得不制作人口,纬度和经度字符串,否则我就无法获得所需的输出。 我希望他们按顺序为int,double和double。

public class City { String countrycode; String city; String region; String population; String latitude; String longitude; public City (String countrycode, String city, String region, String population, String latitude, String longitude) { this.countrycode = countrycode; this.city = city; this.region = region; this.population = population; this.latitude = latitude; this.longitude = longitude; } public String toString() { return this.city + "," + this.population + "," + this.latitude + "," + this.longitude; } } 

我怀疑它与我如何创建数组列表有关。 是否有办法使列表中的某些元素具有不同的类型? 我尝试将其更改为ArrayList并更改City类中的数据类型,但它仍然给了我很多错误。

 public class Reader { In input = new In("file:world_cities.txt"); private static City cityInfo; public static void main(String[] args) { // open file In input = new In("world_cities.txt"); input = new In("world_cities.txt"); try { // write output to file FileWriter fw = new FileWriter("cities_out.txt"); PrintWriter pw = new PrintWriter(fw); int line = 0; // iterate through all lines in the file while (line < 47913) { // read line String cityLine = input.readLine(); // create array list ArrayList cityList = new ArrayList(Arrays.asList(cityLine.split(","))); // add line to array list cityList.add(cityLine); // increase counter line += 1; // create instance cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), cityList.get(3), cityList.get(4), cityList.get(5)); System.out.println(cityInfo); // print output to file pw.println(cityInfo); } // close file pw.close(); } // what is printed when there is an error when saving to file catch (Exception e) { System.out.println("ERROR!"); } // close the file input.close(); } } 

如果您按如下方式声明列表,则可以将任何引用类型的实例放入其中:

  List list = new ArrayList<>(); 

但缺点是当你从列表中获取一个元素时,元素的静态类型将是Object ,你需要输入它所需的类型。

另请注意,您不能将intdouble放入List 。 原始类型不是引用类型, List API要求元素是引用类型的实例。 您需要使用相应的包装类型; 即IntegerDouble


看看你的更多代码,我发现了这个:

 ArrayList cityList = new ArrayList(Arrays.asList(cityLine.split(","))); 

如果将列表更改为List ,其中对象为IntegerDouble ,则无法像这样构建列表。

事实上,我看的越多,我就越认为你根本不需要列表。 你应该做这样的事情:

  // read line String cityLine = input.readLine(); String[] parts = cityLine.split(","); // increase counter line += 1; // create instance cityInfo = new City(parts[0], parts[1], parts[2], Integer.parseInt(parts[3]), Double.parseDouble(parts[4]), Double.parseDouble(parts[5])); 

注意:根本没有List

您可以在将字符串值传递到新的City对象之前解析它们。 然后,您可以将City的构造函数和变量更改为int,double和double。

 int pop = Integer.parseInt(cityList.get(3)); double latitude = Double.parseDouble(cityList.get(4)); double longitude = Double.parseDouble(cityList.get(5)); cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), pop, latitude, longitude); 

通过查看City类,您已定义成员具有不同的原始数据类型。 当您读取文件以创建City时,您需要使用构造函数中定义的数据类型传递构造函数参数。

修改您的City类,如下所示:

 public class City { String countrycode; String city; String region; int population; double latitude; double longitude; ... 

请尝试以下方法:

 cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), Integer.parseInt(cityList.get(3)), Double.parseDouble(cityList.get(4)), Double.parseDouble(cityList.get(5))); 

这将把字符串转换为int,并根据City类的要求加倍。

我相信不,但你可以这样做

 public class City { String countrycode; String city; String region; String population; double latitude; double longitude; public City (String countrycode, String city, String region, String population, double latitude, double longitude) { this.countrycode = countrycode; this.city = city; this.region = region; this.population = population; this.latitude = latitude; this.longitude = longitude; } public String toString() { return this.city + "," + this.population + "," + this.latitude + "," + this.longitude; } } public class Reader { In input = new In("file:world_cities.txt"); private static City cityInfo; public static void main(String[] args) { // open file In input = new In("world_cities.txt"); input = new In("world_cities.txt"); try { // write output to file FileWriter fw = new FileWriter("cities_out.txt"); PrintWriter pw = new PrintWriter(fw); int line = 0; // iterate through all lines in the file while (line < 47913) { // read line String cityLine = input.readLine(); // create array list ArrayList cityList = new ArrayList(Arrays.asList(cityLine.split(","))); // add line to array list cityList.add(cityLine); // increase counter line += 1; // create instance cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), cityList.get(3), Doule.parseDouble(cityList.get(4)), Doule.parseDouble(cityList.get(5))); System.out.println(cityInfo); // print output to file pw.println(cityInfo); } // close file pw.close(); } // what is printed when there is an error when saving to file catch (Exception e) { System.out.println("ERROR!"); } // close the file input.close(); } } 

你可以这样做:

读者群

 import java.io.FileWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; public class Reader { In input = new In("file:world_cities.txt"); private static City cityInfo; public static void main(String[] args) { // open file // In input = new In("world_cities.txt"); // input = new In("world_cities.txt"); try { // write output to file FileWriter fw = new FileWriter("cities_out.txt"); PrintWriter pw = new PrintWriter(fw); int line = 0; // iterate through all lines in the file while (line < 47913) { // read line String cityLine = input.readLine(); // create array list ArrayList cityList = new ArrayList(Arrays.asList(cityLine.split(","))); // add line to array list cityList.add(cityLine); // increase counter line += 1; // create instance cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), Integer.parseInt(cityList.get(3)), Double.parseDouble(cityList.get(4)), Double.parseDouble(cityList.get(5))); System.out.println(cityInfo); // print output to file pw.println(cityInfo); } // close file pw.close(); } // what is printed when there is an error when saving to file catch (Exception e) { System.out.println("ERROR!"); } // close the file input.close(); } } 

城市类

 public class City { String countrycode; String city; String region; int population; double latitude; double longitude; public City (String countrycode, String city, String region, int population, double latitude, double longitude) { this.countrycode = countrycode; this.city = city; this.region = region; this.population = population; this.latitude = latitude; this.longitude = longitude; } public String toString() { return this.city + "," + this.population + "," + this.latitude + "," + this.longitude; } } 

是否有办法使列表中的某些元素具有不同的类型?

可以使用具有不同类型的元素的List ,但是如果使用String.split()填充它则不会 – 因为它返回String[]

您可以使用Java原始类型包装器上的valueOf方法String.split()返回的字符串转换为所需类型,例如……

 Integer population = Integer.valueOf("1234567"); // Returns 1234567 as an Integer, which can autobox to an int if you prefer 

就像Stephen建议的那样,你可以使用List ,除此之外,你可以将String传递给City,但让City自己处理数据类型。

 public class City { String countrycode; String city; String region; int population; double latitude; double longitude; public City (String countrycode, String city, String region, String population, String latitude, String longitude) { this.countrycode = countrycode; this.city = city; this.region = region; try{ this.population = Integer.parseInt(population); this.latitude = Double.parseDouble(latitude); this.longitude = Double.parseDouble(longitude); }catch(Exception e){ e.printStacktrace(); } } public String toString() { return this.city + "," + this.population + "," + this.latitude + "," + this.longitude; } } 

顺便说一句,你已经发起了两次。

 In input = new In("world_cities.txt"); input = new In("world_cities.txt"); 

将其修改为

 In input = new In("world_cities.txt");