Java ArrayList with multiple data types

Creating an array list of multiple data types in Java

Some of the file I'm working with: //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.

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:

Notice: there is no List there at all!!

You could parse the string values before they are passed into a new City object. Then you could change the constructor and variables within a City to be an int, double, and 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];

By looking at the City class you have defined the members are of different primitive data types. When you read the file to create a City, you need to pass the constructor parameters with the defined data types as in your constructor.

Modify your City class as below :

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

Try the following :

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]]];

This will convert the Strings to int and double as desired by the City class.

I believe no, but you can do this

You can do something like this:

READER CLASS

CITY CLASS

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; } }

Is there a way to make it so that some of the elements of the list are of a different type?

It is possible to have a List with elements of different type, but not if you're populating it with String.split[] -- because that returns a String[].

You can convert the strings you get back fro String.split[] into the desired types with the valueOf methods on the Java primitive type wrappers, for example...

Like Stephen suggested, you can use List, besides that, you can just pass String to City but let City itself to handle the datatype.

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; } }

Btw, you have initiated In twice.

In input = new In["world_cities.txt"]; input = new In["world_cities.txt"];

Modify it to

In input = new In["world_cities.txt"];

Video liên quan

Chủ Đề