• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing and sorting arraylist of arraylist

 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i would like some help for my problem. Here is an exemple of the arraylist of arraylist i have got :


My problem is that, from this arrayList, i have to compare the string element of each list to see if they are equals. Then if they are equals, i have to add the integer of each column with equal string between them and finally remove one of the duplicate array, so for the exemple the result would be :
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know if my algorithm is good but i manage to have an array of array containing the doubled data :


Now i would like some help for adding column together then merge the array with my original ArrayList of ArrayList.

Thank you for any help
 
Subhash Bhushan C
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved your problem with a combination of ArrayList and Map. In fact, I used Arraylist just to illustrate to you. You can very well skip ArrayList completely, and use Map along with your class.

You can read up on Collections here: Trail: Collections (The Java Tutorial)

Here is the class containing your data:


And here is a small implementation to show how the adding is done:


And this is the output I get:

----------------------
ArrayList Data
----------------------
FIRST-1-0-2-0-3-0-4-0
SECOND-3-0-4-0-5-0-6-0
FIRST-5-0-6-0-7-0-8-0
----------------------


----------------------
Map Data
----------------------
SECOND-3-0-4-0-5-0-6-0
FIRST-6-0-8-0-10-0-12-0
----------------------


[ April 22, 2008: Message edited by: Subhash Bhushan ]
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks greatly for this, it will help me a lot. Pity i m not using java 5 but still 1.4.2, but i didnt think about working with map which seems to be very convenient in this case, thanks, ill try work through your exemple and adapt it to my case.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic