java 创建集合类数组,在Java中创建多个数据类型的数组列表

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 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 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();

}

}

解决方案

If you declare the list as follows, you can put instances of any reference type into it:

List 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 cityList =

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

If you change the list to List 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!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值