How to use java.Set

Posted by owca on Stack Overflow See other posts from Stack Overflow or by owca
Published on 2010-03-22T05:38:21Z Indexed on 2010/03/22 5:41 UTC
Read the original article Hit count: 449

Filed under:
|
|
|

I'm trying to make it working for quite some time,but just can't seem to get it. I have object Tower built of Block's. I've already made it working using arrays, but I wanted to learn Set's. I'd like to get similar functionality to this:

public class Tower {


public Tower(){
}

public Tower add(Block k1){

    //(...)
    //if block already in tower, return "Block already in tower"
}

public Tower delete(Block k1){

    //(...)
    //if block already dleted, show "No such block in tower"
}

}

Someone gave me some code, but I constantly get errors when trying to use it :

Set<Block> tower = new HashSet<Block>();

boolean added = tower.add( k1 );
if( added ) {
System.out.println("Added 1 block.");
} else {
System.out.println("Tower already contains this block.");
}

How to implement it ?

© Stack Overflow or respective owner

Related posts about java

Related posts about collections