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.
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!!!
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 ]
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.
Your mother was a hamster and your father was a tiny ad: