keeps finding a "Object" not the real type.. I am really lost.. :D

The calls to other objects work fine, I have testing all of them.. but! when I compile I get a error about finding a "object" type and not a String type at

for(String blackStone : blackUnits.keySet())

{

if( getSetLib(blackStone) == 0 ){ removeSet.add(blackStone);}

}

}

this is the whole class

import java.util.ArrayList;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Set;

publicclass Stat

{

private Token[][] board;

privateint bSize;

private HashMap<String, HashSet> unitsWhite;

private HashMap<String, HashSet> unitsBlack;

double last;

public Stat(Token[][] goBoard,int size)

{

board = goBoard;

bSize = size;

unitsWhite =new HashMap< String , HashSet>();

unitsBlack =new HashMap< String , HashSet>();

last = bSize -1;

}

/**

* Delimiter is /

*/

public HashSet getLib(int x ,int y)

{

HashSet liberties =new HashSet<String>();

//error catch

if(board[x][y] ==null){return liberties;}

int tempX = x;

int tempY = y;

//error catch for out of rage if size ect..

for(int i = 0 , liby = y ,libx = x ; i <= 3; i++)

{

if(i == 0){libx = -1; liby = 0;}

if(i == 1){libx = 0; liby = -1;}

if(i == 2){libx = 1; liby = 0;}

if(i == 3){libx = 0; liby = 1;}

tempX = x+libx;

tempY = y+liby;

if(tempX >= 0 || tempY >= 0)

{

if( tempX <= last || tempY <= last )

{

if( (tempX < 0 || tempY < 0) || ( tempX >= last || tempY >= last ) )

{

tempX = 0;

tempY = 0;

}

else

{

if( board[tempX][tempY] ==null){ liberties.add(tempX+"/"+tempY);}

}

}

}

}

return liberties;

}

publicvoid getTer()

{

}

publicvoid checkForCaptured()

{

Token black =null;

Token white =null;

HashMap blackUnits;

HashMap whiteUnits;

HashSet removeSet ;

removeSet =new HashSet<String>();

//look for a Token on the board and if you find one look for any dead Tokens

//looks for a Token

for(int x=0; x < bSize;x++)

{for(int y=0 ; y < bSize;y++)

{if(board[x][y] !=null)

{

if (black ==null)if( board[x][y].getColor().equals("black") ) black = board[y][x];

if (white ==null)if( board[x][y].getColor().equals("white") ) white = board[y][x];

}

}

}

System.out.println("this is black tokens color" +black.getColor());

//looks for a dead unit

if(black !=null)

{ blackUnits = black.getUnits();

System.out.println("black :" +blackUnits.keySet().size() ) ;

for(String blackStone : blackUnits.keySet())

{

if( getSetLib(blackStone) == 0 ){ removeSet.add(blackStone);}

}

}

if(white !=null)

{ whiteUnits = white.getUnits();

System.out.println(" white: "+whiteUnits.keySet().size() );

for( String whiteStone : whiteUnits.keySet() )

{if( getSetLib(whiteStone) == 0 ){ removeSet.add(whiteStone);}}

}

board.removeUnitsSet(removeSet);

}

publicint getSetLib(HashSet Units)

{

return 0;

}

}

someone point me in the correct direction PLZ! I don't mind RTFM if you give me a FM and page/line to look at.

THANKS!

Message was edited by:

monji112000

[7713 byte] By [bronze-starDukes] at [2007-11-26 12:06:39]
# 1

The compiler doesn't know that you're only putting Strings in as keys. HashMap takes references to Object as keys and hence returns references to Object in keySet(). Note, however, that the object itself is still what you put in there. But the compiler only has the declared return type of the method to look at.

// pre 1.5

for (Iterator iter = set.iterator(); iter.hasNext();) {

String key = (String)iter.next(); // cast Object to String. Only works if you actually put a String in

// do stuff with key

}

// 1.5, if you don't use generics

for (Object obj : set) {

String key = (String)obj;

// do stuff with key

}

// 1.5, with generics

Set<String> set = ...;

for (String key : set) {

// do stuff with key

}

Since you have a Map, you'll have Map<String, SomeValueType>.

http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

platinumsta at 2007-7-7 13:38:49 > top of Java-index,Archived Forums,Socket Programming...
# 2
The getSetLib method expects a HashSet as a parameter. You are passing blackStone which is a String.
goldstar at 2007-7-7 13:38:49 > top of Java-index,Archived Forums,Socket Programming...
# 3
thank you I will read the links you provided and go over what you said.
bronzestar at 2007-7-7 13:38:49 > top of Java-index,Archived Forums,Socket Programming...
# 4
yah I noticed that and I fixed it. BUt the issue was generics and I guess I need to read more about it.
bronzestar at 2007-7-7 13:38:49 > top of Java-index,Archived Forums,Socket Programming...