PDA

View Full Version : Java qyestion (should be easy)



doc_bean
03-07-2006, 13:25
okay, I need a Vector (or another type of array) to contain only a certain Class of Objects, so that I can use the class operaters on any element of the vector without the programming environment (and possibly compiler) whining.

I was pretty sure that this was possible using the Vector thingie from java.util.* , but i can't find the right syntax.


HELP !


:help:

drone
03-07-2006, 17:13
Not quite sure what you want, but maybe this will help:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html
Syntax and access commands for the Vector class.

Can't help you much more than this, I'm an embedded C,C++ guy. Java makes no sense to me. :inquisitive:

doc_bean
03-07-2006, 18:15
Let's say I want a collection (an array or a vector or whatever) of Strings, and I want to make sure there are no Objects of other Classes (besides String) in my collection. I'm not able to do that eventhough it should be one of the most basic operations imho.

The more I learn about Java the less sense it makes. :help:

Sasaki Kojiro
03-07-2006, 18:28
Have you checked the Java tutorial on collections?

http://java.sun.com/docs/books/tutorial/collections/index.html


Coding in eclipse (free) can help catch syntax errors (it underlines them in red as you type, just like word :2thumbsup: )

http://www.eclipse.org/

Krypta
03-07-2006, 19:09
Fekking logged me out, after I finished writing a long post.:furious3:

Anyways, real quick as I don't have the time to type it out again, perhaps tonight if I can get a minute.

Try
boolean contains(Object elem)
Tests if the specified object is a component in this vector.
In the api that Drone linked too. That method looks like what you are looking for.

drone
03-07-2006, 21:52
Reading through the Vector page, looks like Java vectors are similiar to arrays of void pointers in C. Any class object can be added to a vector. Is this correct?

If so, can you use the getClass() method to do what you want? Verify the class then cast appropriately to remove the compile problems?

Phatose
03-08-2006, 07:58
Should be able to use a generic for this.

Vector <String> myVector;

doc_bean
03-08-2006, 13:07
Should be able to use a generic for this.

Vector <String> myVector;

Yes ! This was what i was looking for. Muchos Gracias ! :2thumbsup: