java - Working with objects in array -


i got text file hundred of lines. this: unitname unitpackage unitprice. got sort , sum up. example got these 4 lines:

foo1 01abc 30.00  foo2 02abc 31.50  foo1 04abc 35.00  foo1 01abc 30.00 

after work on have this:

foo1 2 (01abc) 60.00  foo2 1 (02abc) 31.50  foo1 1 (04abc) 35.00 

my idea make product class (string unitname, string unitpackage, double unitprice) , put arraylist that:

list<product> products = new arraylist<>();      while (inputfile.hasnextline()) {         product product = new product(inputfile.next(), inputfile.next(), inputfile.nextdouble());         products.add(product);     } 

until moment works fine, dont idea, how sort things. it's overall concept way? please advice.

there many ways array list sorted. in particular case, put every item list, maybe simplest form use map implementation, i.e. hashmap, instead of arraylist.

after loop of insertions can sorted list through values() method of map.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -