my dog learned polymorphism*
The moose likes Beginning Java and the fly likes using the getters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using the getters" Watch "using the getters" New topic
Author

using the getters

Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
I need to put the data in the variable that I have setters and getters for into a vector, then write it back out. How would I use or would I use the getters to put the data into the vector? If not, how would I get the data to the vector. Would this be a separate class?
Sloan Bowman
Ranch Hand

Joined: Jan 21, 2003
Posts: 107
What I beleive you are trying to do is create a vector (or I recommend a ArrayList) of objects and then display or use those objects (Beans) to display or query etc.. To do this is actually very simple. Just create a Bean object say CarBean with lets say color, engine, etc.. as the instance variables. You would run a query or create the objects lets say with car.setColor("blue"); car.setEngine("v6"); then add that object to the Vector/ArrayList. Create as many as you need and then return that Vector or ArrayList as a return value from that method. Then get and cast the Objects to use the data. If you need an example let me know.
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
I think I need an example. I need to use Vector because that is what the instructor has asked us to do. Here is what I have now:
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
Your ButtonListener is missing the method header for actionPerformed(), but lets assume that's a typo and it is there. Note that the code in this method creates an AccountTransaction and then does nothing with it. When the method exits, the newly created AccountTransaction vanishes. It would make a lot more sense to save it by, say, adding it to a collection of some kind. Could it be that you are expected to create a Vector named, say, "transactions"; and as a last line of actionPerformed() have "transactions.add(trans);"?


The Inner that is named is not the true Inner.
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Yes, I did just leave off the actionPerformed(), I do have that in the program. If I use transaction.add(trans), will that add both grpAcctNumber and SSN to the vector or just the last one?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Technically, it will not add wither the grpAccoutnNumebr or the SSN to the Vector. It will add the AccountTransaction object. Since both fields are attributes of the AccountTransaction object, they will essentially both be in the Vector, but you access them through the AccountTransaction object.
This comes down to what data belongs to what object. If you think of this as being displayed in a tree format (and with multiple account Transaction objects added to the vector), it would look like:

So, to find the account number of the third transaction in the vector, you call:

Remember, vectors are zero-indexed, so the first object is at index 0, the second at index 1, etc.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
I get a compile error, cannot resolve symbol, and the carrot points to the period before objectAt(1); Why can't this compile ok?
transaction is defined as the vector.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
[b]I get a compile error, cannot resolve symbol, and the carrot points to the period before objectAt(1); Why can't this compile ok?
transaction is defined as the vector.[b]
Class Vector doesn't have a method objectAt(). Perhaps you meant the method elementAt(int index)? But then again, Vector is a legacy collection and you should not use it in the first place.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Sorry -- that was my fault. It's been so long since I've used the Vector class that I forget what is what ... objectAt was what I used for so long in Objective-C.
Candy, I strongly urrge you to talk with your professor about using java.util.List classes. It is to the benefit of everybody that you use a List class. They are faster, can be made thread-safe, and we won't then have a whole classful of people using the outmoded Vectors. And you might even impress you professor by showing that you know a little bit about the finer points of Java
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using the getters