在 Java 中创建多种数据类型的数组列表 [英] Creating an array list of multiple data types in Java
问题描述
我正在处理的一些文件:http://pastebin.com/WriQcuPs>
目前我必须制作人口、纬度和经度字符串,否则我将无法获得所需的输出.我希望它们按顺序是 int、double 和 double.
公共类城市{字符串国家/地区代码;弦城;字符串区域;字符串人口;字符串纬度;字符串经度;公共城市(字符串国家代码,字符串城市,字符串地区,字符串人口,字符串纬度,字符串经度){this.countrycode = 国家/地区代码;this.city = 城市;this.region = 区域;this.population = 人口;this.latitude = 纬度;this.longitude = 经度;}公共字符串 toString() {返回 this.city + "," + this.population+ "," + this.latitude + ","+ this.longitude;}}
<小时>
我怀疑这与我创建数组列表的方式有关.有没有办法使列表中的某些元素具有不同的类型?我尝试将其更改为 ArrayList
并更改 City
类中的数据类型,但它仍然给了我很多错误.
公共类阅读器{In input = new In("file:world_cities.txt");私有静态城市城市信息;公共静态无效主(字符串 [] args){//打开文件In input = new In("world_cities.txt");input = new In("world_cities.txt");尝试 {//将输出写入文件FileWriter fw = new FileWriter("cities_out.txt");PrintWriter pw = new PrintWriter(fw);整数行 = 0;//遍历文件中的所有行而(行 < 47913){//读取行String cityLine = input.readLine();//创建数组列表ArrayListcityList = new ArrayList(Arrays.asList(cityLine.split(",")));//添加行到数组列表cityList.add(cityLine);//增加计数器线 += 1;//创建实例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);//打印输出到文件pw.println(cityInfo);}//关闭文件pw.close();}//保存到文件时出错时打印的内容捕获(异常 e){System.out.println("错误!");}//关闭文件input.close();}}
如果声明列表如下,则可以将任何引用类型的实例放入其中:
列表<对象>list = new ArrayList<>();
但缺点是当您从列表中获取一个元素时,该元素的静态类型将是 Object
,您需要将其类型转换为您需要的类型.
另请注意,您不能将 int
或 double
放入 List
.原始类型不是引用类型,List
API 要求元素是引用类型的实例.您需要使用相应的包装器类型;即 Integer
和 Double
.
查看您的更多代码,我发现了这一点:
ArrayList城市列表 =new ArrayList(Arrays.asList(cityLine.split(",")));
如果您将列表更改为 List
,其中对象是 Integer
或 Double
,您将无法像这样建立你的清单.
事实上,我越看这个,我就越觉得你根本不需要清单.你应该做这样的事情:
//读取行String cityLine = input.readLine();String[] 部分 = cityLine.split(",");//增加计数器线 += 1;//创建实例cityInfo = new City(parts[0],parts[1],parts[2],Integer.parseInt(parts[3]),Double.parseDouble(parts[4]),Double.parseDouble(parts[5]));
注意:那里根本没有List
!!
Some of the file I'm working with: http://pastebin.com/WriQcuPs
Currently I had to make the population, latitude, and longitude strings or else I wouldn't get the desired output. I want for them to be int, double, and double in that order.
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;
}
}
I suspect it has something to do with how I created the array list. Is there a way to make it so that some of the elements of the list are of a different type? I tried changing it to ArrayList<City>
and changing the data types in the City
class but it still gave me a ton of errors.
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<String> cityList = new ArrayList<String>(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();
}
}
If you declare the list as follows, you can put instances of any reference type into it:
List<Object> list = new ArrayList<>();
But the downside is that when you get an element from the list, the static type of the element will be Object
, and you will need to type cast it to the type that you need.
Also note, that you can't put an int
or a double
into a List
. Primitive types are not reference types, and the List
API requires the elements to be instances of reference types. You need to use the corresponding wrapper types; i.e. Integer
and Double
.
Looking at more of your code, I spotted this:
ArrayList<String> cityList =
new ArrayList<String>(Arrays.asList(cityLine.split(",")));
If you change the list to List<Object>
where the objects are either Integer
or Double
, you won't be able to build your list like that.
In fact, the more I look this, the more I think that you don't need a list at all. You should be doing something like this:
// 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]));
Notice: there is no List
there at all!!
这篇关于在 Java 中创建多种数据类型的数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!