• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access to methods in vector objects

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks!!
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to call the method in one of the objects in the vector it should be more like:

to get the instance variable, I think it would be:

You can of course loop through the objects incrementing the index up to m.size() - 1
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

a[i].callMethod();

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.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hosh Nasi,

Welcome to the Ranch!

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?

I think you may benefit from taking a look at Sun's Collections Tutorial.

Jules
[ September 11, 2004: Message edited by: Julian Kennedy ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Any ideas?



This windows is nto liking my code indention. Sorry if it looks jumbled
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Jules
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic