java - Remove duplicate string from ArrayList -


i have arraylist of strings duplicate elements. every 3 rows make entity:

 2 550111.55 1, 3 
 3 550111.55 1, 2, 3 
 2 155.55 1, 2 
 3 550111.55 2, 3, 1 
 2 155.55 2, 1 
 2 550111.55 3, 1 
 3 550111.55 3, 1, 2 

all elements strings. first number says number of participants, second says value, , third elements make up. have duplicates

 3 550111.55 3, 1, 2 

and

 3 550111.55 1, 2, 3 

and

 3 550111.55 2, 3, 1 

the number of elements says number of duplicates, must remove duplicates, in case, delete 1 instance, example:

 3 550111.55 2, 3, 1 

i trying :

arraylist<string> lista_rezultata1=new arraylist<string>(); lista_rezultata1=lista_rezultata; //set count 3 in arraylist since data need split , evaluate on third place int number1=0; int number2=0; string[] part1=null; string[] part2=null; int removefromlist=0; (int = 0; < lista_rezultata.size(); i++) {     if(number1==3)number1=0;     if(number1==2)//if third element 1 "," split     {         part1=lista_rezultata.get(i).tostring().split(",");     }      (int j = 0; j < lista_rezultata1.size(); j++) {         if(number2==3)number2=0;           if(number2==2 && i!=j && number1==2)//if third element 1 "," split, first must splited         {             part2=lista_rezultata1.get(j).tostring().split(",");              (int k = 0; k < part1.length; k++) {                 (int l = 0; l < part2.length; l++) {                     if(part1.length==part2.length)   //if have same number of elements                     {                          if(part2[l].equals(part1[j]))                         {                             .....some code                         }                     }                 }             }         }          number2=number2+1;        }     number1=number1+1;  } 

so hoping there better way ...

your approach wrong i'm afraid...

  • create class represent items
  • override equals() method returns true if item values same
  • override hashcode() method produce int value based on same values compared in equals() method (so hashcode "agrees with" equals)
  • use set<item> save items - sets use equals() ensure there no duplicates

you may wish use linkedhashset set implementation:

set<item> set = new linkedhashset<item>(); 

a linkedhashset ensure iteration order of elements in same order inserted.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

php - Dynamic url re-writing using htaccess -

java - Multi-Label Document Classification -