How to use java.Set
- by owca
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 ?