Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an ArrayList of Strings with duplicate elements. Every 3 rows make an 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 the elements are Strings. The first number says the number of participants, the second says the value, and the third which elements make it up. So I have duplicates like

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 the number of duplicates, so I must remove all the duplicates, in this case, delete all but one instance, for example:

3
550111.55
2, 3, 1

I am trying to do it like this :

ArrayList<String> lista_rezultata1=new ArrayList<String>();
lista_rezultata1=lista_rezultata;
//set to count to 3 in arraylist since data I need to split and evaluate are always on the third place
int number1=0;
int number2=0;
String[] part1=null;
String[] part2=null;
int removefromlist=0;
for (int i = 0; i < lista_rezultata.size(); i++) {
    if(number1==3)number1=0;
    if(number1==2)//If it is a third element it is the one with "," that I split
    {
        part1=lista_rezultata.get(i).toString().split(",");
    }

    for (int j = 0; j < lista_rezultata1.size(); j++) {
        if(number2==3)number2=0; 

        if(number2==2 && i!=j && number1==2)//If it is a third element it is the one with "," that I split, first must already be splited
        {
            part2=lista_rezultata1.get(j).toString().split(",");

            for (int k = 0; k < part1.length; k++) {
                for (int l = 0; l < part2.length; l++) {
                    if(part1.length==part2.length)   //if they have the same number of elements
                    { 
                        if(part2[l].equals(part1[j]))
                        {
                            .....some code
                        }
                    }
                }
            }
        }

        number2=number2+1;   
    }
    number1=number1+1; 
}

So I am hoping there is a better way ...

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Your approach is all wrong I'm afraid...

  • Create a class to represent your items
  • Override the equals() method that returns true if your item values are the same
  • Override the hashCode() method to produce an int value based on the same values you compared in your equals() method (so hashCode "agrees with" equals)
  • Use a Set<Item> to save your items - sets use equals() to ensure there are no duplicates

You may wish to use a LinkedHashSet for you Set implementation:

Set<Item> set = new LinkedHashSet<Item>();

A LinkedHashSet will ensure the iteration order of its elements in the same order they were inserted.

share|improve this answer
    
You are right about the classes but what am I to do with Strings that go like 1,2,3 and 2,3,1 how am I to present these and do not mess up the order in which they go... –  Jovo Krneta May 22 '13 at 0:54
    
Use a LinkedHashSet, which iterate in inserted order –  Bohemian May 22 '13 at 2:16
add comment

If the elements parts had their members in order, then you would have actual duplicates, which should simplify things.

share|improve this answer
    
It is importand that they are kept in the same order if I have 1,2,3 then 2,3,1 and 3,1,2 it is important that I am left with at lest one combination and they must be within same entity - the group of 3 , first is the number of participants , second is the value and the third are the ones that make the circle , these are in a fact results from graph database ... –  Jovo Krneta May 22 '13 at 0:45
add comment

You you should probably use ArrayLists instead. However even if you prefer not to, you can check if an array contains a certain element like this:

Arrays.asList(yourArray).contains(element);
share|improve this answer
    
I am splitting the String 1,2,3 it may be that there is a combination like 1,2,3,4 and it is not the same ...It does contain it , all the elements but it is not for removing.. –  Jovo Krneta May 22 '13 at 0:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.