Using Vector in Java 1.5

D

Dan Upton

I have a simple class that basically stores some data in a char[][] plus
some extra useful things about it, and in an attempt to pass an
arbitrary number of these objects back and forth through method calls
I'm storing them all in a Vector. When I compile, javac tells me I
should recompile with -Xlint:unchecked ; when I do that, it gives me the
warning

PuzzleInput.java:157: warning: [unchecked] unchecked call to add(E) as a
member of the raw type java.util.Vector
pieces.add(newPiece);

For now, I fixed it by changing my initialization of pieces to be

Vector<Piece> = new Vector<Piece>();'

but I guess my actual question is if I know that my vector is only going
to be containing this one particular type of object in it, is there any
particular reason I should type the vector (other than to get the
compiler to leave me alone)?
 
J

jimi_usenet

Dan said:
but I guess my actual question is if I know that my vector is only going
to be containing this one particular type of object in it, is there any
particular reason I should type the vector (other than to get the
compiler to leave me alone)?

Think of it like this:

Let's say you have some class, with an private variable myDog, that is
of type Dog. And you have a set- and a get-method for that variable. If
you know that you only will use Dog objects in the program, is there
any particular reason you should leave the type as Dog, instead of just
setting it to Object?
If you answer yes, what is the big difference from your case?

/Jimi
 
J

Jeff Schwab

Dan said:
I have a simple class that basically stores some data in a char[][] plus
some extra useful things about it, and in an attempt to pass an
arbitrary number of these objects back and forth through method calls
I'm storing them all in a Vector. When I compile, javac tells me I
should recompile with -Xlint:unchecked ; when I do that, it gives me the
warning

PuzzleInput.java:157: warning: [unchecked] unchecked call to add(E) as a
member of the raw type java.util.Vector
pieces.add(newPiece);

For now, I fixed it by changing my initialization of pieces to be

Vector<Piece> = new Vector<Piece>();'

but I guess my actual question is if I know that my vector is only going
to be containing this one particular type of object in it, is there any
particular reason I should type the vector (other than to get the
compiler to leave me alone)?

That's the point. You intend that the vector should only hold Pieces;
by declaring Vector<Piece>, you make this clear to everyone who reads
the code, and tell the compiler to double-check for you. If you didn't
know that the vector should only hold Pieces, you would not declare it
Vector<Piece>.
 
R

Roedy Green

but I guess my actual question is if I know that my vector is only going
to be containing this one particular type of object in it, is there any
particular reason I should type the vector (other than to get the
compiler to leave me alone)?

It prevents you or anyone else adding something else to it. Right
NOWi it is obvious it should only contain that one type of object,but
it might not be later when you are making some emergency fix.

Think of <...> as a way of clearly documenting that restriction.
 
M

MShillinger

Dan said:
I have a simple class that basically stores some data in a char[][] plus
some extra useful things about it, and in an attempt to pass an
arbitrary number of these objects back and forth through method calls
I'm storing them all in a Vector. When I compile, javac tells me I
should recompile with -Xlint:unchecked ; when I do that, it gives me the
warning

PuzzleInput.java:157: warning: [unchecked] unchecked call to add(E) as a
member of the raw type java.util.Vector
pieces.add(newPiece);

For now, I fixed it by changing my initialization of pieces to be

Vector<Piece> = new Vector<Piece>();'

but I guess my actual question is if I know that my vector is only going
to be containing this one particular type of object in it, is there any
particular reason I should type the vector (other than to get the
compiler to leave me alone)?

So you can avoid the cast when getting elements (i.e when iterating)
since you are specifying the collection in homogeneous:

private Vector<Piece> pieceList = new Vector<Piece>();'


for (Piece myPiece: pieceList){
//Notice there is no cast
myPiece.doSomething();
}

as opposed to

for ( int i = 0; i < peiceList.size(); i++){
//The following cast could fail if element at i
//is not an instance of Piece
Piece myPiece = (Piece)pieceList.get(i);
}

or

for ( Iterator i = pieceList.iterator(); i.hasNext();){
//The following cast could fail if next()
//does not return an instance of Piece
Piece myPiece = (Piece)i.next();
}

If you intend to have a hetrogenious collection use:

private Vector<Object> pieceList = new Vector<Object>();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top