arraylist - Creating an array list of multiple data types in Java -


some of file i'm working with: http://pastebin.com/wriqcups

currently had make population, latitude, , longitude strings or else wouldn't desired output. want them int, double, , double in 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 has how created array list. there way make of elements of list of different type? tried changing arraylist<city> , changing data types in city class still gave me 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 file         filewriter fw = new filewriter("cities_out.txt");         printwriter pw = new printwriter(fw);          int line = 0;          // iterate through lines in 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 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 file              pw.println(cityinfo);          }          // close file         pw.close();         }          // printed when there error when saving file         catch (exception e) {             system.out.println("error!");         }          // close file         input.close();     } } 

if declare list follows, can put instances of reference type it:

    list<object> list = new arraylist<>(); 

but downside when element list, static type of element object, , need type cast type need.

also note, can't put int or double list. primitive types not reference types, , list api requires elements instances of reference types. need use corresponding wrapper types; i.e. integer , double.


looking @ more of code, spotted this:

arraylist<string> citylist =      new arraylist<string>(arrays.aslist(cityline.split(","))); 

if change list list<object> objects either integer or double, won't able build list that.

in fact, more this, more think don't need list @ all. should doing 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 no list there @ all!!


Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -