Double entries

Hey,

I've got a "get rid of double entries" method, that just doesn't do what is expected from it...

Can anyone give a reason to why this doesn't work (and additionally, tell me how to fix it)

private ArrayList <Positie> verwijderDubbel(ArrayList <Positie> oorsp){

for (int i = 0; i < oorsp.size(); i++){

for (int j = 0; j < oorsp.size(); j++){

if ( i!=j && oorsp.get(i).equals(oorsp.get(j)) )

oorsp.remove(i);

}

}

return oorsp;

}

Thanks,

Smetje

[946 byte] By [smetjea] at [2007-11-27 7:27:20]
# 1
Use a Set. It's made for having only one of each entry. http://java.sun.com/docs/books/tutorial/collections/Make sure you override equals and hashCode properly. http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf
jverda at 2007-7-12 19:07:41 > top of Java-index,Java Essentials,New To Java...
# 2
Can you just work with Set < Positie > instead of List < Positie > ? That would solve the problem entirely.
Hippolytea at 2007-7-12 19:07:41 > top of Java-index,Java Essentials,New To Java...
# 3
Why not just add each element to a LinkedHashSet and then retrieve the results? The Set part of it will remove duplicates, and the Linked part of it will keep the elements in the order you add them.
hunter9000a at 2007-7-12 19:07:41 > top of Java-index,Java Essentials,New To Java...
# 4

How is this?

static <T> List <T> removeCopies (List <T> list) {

return new ArrayList <T> (new LinkedHashSet < T > (list));

}

I tweaked this code using Hunter's suggestion.

Message was edited by:

Hippolyte

Hippolytea at 2007-7-12 19:07:41 > top of Java-index,Java Essentials,New To Java...
# 5

I skimmed the link you've given, seems usefull indeed, but here's what I made it. (wich, as you could guess, does not quite work)

public ArrayList <Positie> geefAlleBewegingen(){//geefAlleBewegingen ==> giveAllePossibleMoves

Set <Positie> bewegingen = new HashSet <Positie> ();

/*

* Positie ==> Position, consists of a char and an int, like A4, or G7

*/

char letter = pos.toString().charAt(0);

int cijfer = Integer.parseInt(""+pos.toString().charAt(1));

int a= 0;

while ( (cijfer+a) <= 8){

int b= 0;

while ( (int) (letter+b) <= (int) 'H' ){

Positie mog = new Positie( (cijfer+a) , (char) (letter+b) );

if ( controleerBeweging(mog) )//controleerBeweging ==> checkmovement

bewegingen.add(mog);

b++;

}

int c = 0;

while ( (int) (letter-c) >= (int) 'A' ){

Positie mog = new Positie( (cijfer+a) , (char) (letter-c) );

if ( controleerBeweging(mog) )

bewegingen.add(mog);

c++;

}

a++;

}

int d = 0;

while ( (cijfer-d) > 0 ){

int e= 0;

while ( (int) (letter+e) <= (int) 'H' ){

Positie mog = new Positie( (cijfer-d) , (char) (letter+e) );

if ( controleerBeweging(mog) )

bewegingen.add(mog);

e++;

}

int f= 0;

while ( (int) (letter-f) >= (int) 'A' ){

Positie mog = new Positie( (cijfer-d) , (char) (letter-f) );

if ( controleerBeweging(mog) )

bewegingen.add(mog);

f++;

}

d++;

}

return toArrayList(bewegingen);

}

private ArrayList <Positie> toArrayList(Set <Positie> a){

ArrayList <Positie> al = new ArrayList <Positie> ();

for (Positie x: a)

al.add(x);

return al;

}

what should I do now?

Smetje

smetjea at 2007-7-12 19:07:41 > top of Java-index,Java Essentials,New To Java...
# 6
> what should I do now?Explain exactly what "does not quite work" means.
jverda at 2007-7-12 19:07:41 > top of Java-index,Java Essentials,New To Java...