This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

how to combine between two arraylist if has same value  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i have 2 arraylist as below

al1 - [Consignment, Bank, Custodian, Rejected, Bank]
al2 - [[2, 0, 0, 0, 0, 0, 0], [6, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [4, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0]]

so my case is i have to check al1 there is any duplicate value, if have have combine the value in al2

so expected result is

al1 - [Consignment, Bank, Custodian, Rejected]
al2 - [[2, 0, 0, 0, 0, 0, 0], [6, 1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [4, 0, 0, 0, 0, 0, 0]]

i'm trying to get solution by googling, anyhow how this can be done

thanks in advance
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lists should allow duplicates. you should use Set if you want to remove duplicates.

One way of removing the duplicates is to form a Set object from your List and converting it back to List like below...

 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maintaining data in two parallel arrays is a nightmare. You'd do better by making a single object that can store both the String and the array associated with it. Then you could possibly store them in a map, using the String as the key. First check to see if it is already in the collection, and if so, 'combine' your arrays. If not, simply add it in.
 
Mobio Dev
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found solution as below

for(int i=0;i<al1.size();i++){
for(int j = al1.size()-1; j > i; j--) {
if(al1.get(i).equals(al1.get(j))){
ArrayList temp1 = (ArrayList)al4.get(i);
ArrayList temp2 = (ArrayList)al4.get(j);
for(int k=0;k<temp1.size();k++){
if(!temp2.get(k).equals("0")){
temp1.set(k, temp2.get(k));
}
}
al1.remove(j);
al4.remove(j);
al4.set(i, temp1);
}
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!