I am nto sure if I am doing this right, but I have a vector that contains objects of the same name with diffrent instance variables. These objects also have methods for searching through another vector using the instance variables when constructed.
My question is now that I have the vector built how do I call the method?
I was hoping for something easy like
however it gives a compiler error about arrays
Also I need a good way to loop through a vector searching for matching instance variables in an object.
The syntax you're using (with the brackets) is for Arrays rather than Vectors.
An Array holds any type of Object or primitive, as long as every element is of the same type. Once created, an Array cannot be resized, but you can easily reference its elements by the index in brackets...
Unlike an Array, a Vector resizes as needed. It does have a method elementAt(i), which returns an Object reference. But anything you put in a Vector is automatically upcast to Object, so in order to call a specific method, you need to downcast that Object back to whatever type it actually is...
MyType t = (MyType) v.elementAt(i); t.callMethod();
Actually, I'm guessing that an Array might serve you better here.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Firstly I should point out that java.util.Vector has effectively been superseded by java.util.ArrayList since the advent of the Collections Framework in Java 1.2. Best advice going forward is to use ArrayList. Vector is really only there for backward compatibility.
Having said that you access a Vector as follows:
You would iterate over a Vector as follows:
Since Java 1.2 Vector has implemented the List interface, so you can also use that to access its elements (confusing, huh?):
You can also iterate as follows:
The thing is there's not much point you learning that as it's largely history. You certainly shouldn't do it going forward.
Here's what you should use: ArrayList:
You can iterate as follows:
Notice that the methods for manipulating and iterating over a Vector (2nd pair of examples) and an ArrayList are identical. This is because they both use the List and Iterator interfaces. Cool, huh?
Originally posted by Julian Kennedy: Since Java 1.2 Vector has implemented the List interface, so you can also use that to access its elements (confusing, huh?)
Ah... That explains the redundancy. I've always regarded the Vector as a legacy sort of List, but never understood how it related to the newer Collections.
Joined: Sep 10, 2004
wow julian good stuff!
This is very helpful, goos info here for all
Look back through this I need to cast an object referenced in a List to search through it?
One of my biggest issues is that I am going to have for example a list of twenty objects. All the objects share the same name but have a few variables that when compared create uniqueness. My first step is to sort the list down to say five that share a "Message Type" value of 1. Then then from within the object call another classes method passing in some of is instance variables.
It is harder to explain then code I know It
Once again thanks for the replies. They are spot on and will help me big time!!!
Joined: Sep 10, 2004
Hey Julian I am trying to iterate through my List objects and it seems to be iterateing through all seven but the values arent changing?
This windows is nto liking my code indention. Sorry if it looks jumbled
Joined: Sep 10, 2004
A little update:
I am really confused..
It looks like the right List is passed in, but when the program runs the while is just looping through the values stored in the object that ahs focus, it is not going to the selector objects to retrieve its values?
[ September 14, 2004: Message edited by: Hosh Nasi ]
::Edit:: I determined the Iterator is starting at the top? But, it is only using the last object and not folowing the index of the iterator??[ September 14, 2004: Message edited by: Hosh Nasi ] [ September 14, 2004: Message edited by: Hosh Nasi ]
Joined: Aug 02, 2004
Are you sure that the 7 objects in the list are actually different? I doubt that the iterator is behaving as you suggest. It's not clear to me what you're trying to do. If you're still struggling perhaps you could put together a simple example that I can understand.