Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector

 
Joey Alencar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to know how can I implement a Vector (or anything like this) of a Class. It's like a array of a class.
Thanks.
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you elaborate? Do you mean how to manipulate instances of your class using a vector or what?

Bosun
 
Joey Alencar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one class and I'd like to create a Vector (or array) of this class. I'd like to know how can I implement and manipulate this (input and get values).
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm.. at the risk of being too obvious In this example, 'v' is a Vector containing instances of your class.

Did you mean that you wanted your class to *be* a Vector-like object? ie:
In this case, you can extend Vector ie: "public class MyClass extends Vector"
 
Joey Alencar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but how can I get the values of my class's variables using this Vector?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the values of your class variables, you need to cast the contents of the vector to the appropriate type, in this case your class type. You can use the enumeration interface to go through the vector.
for(Enumeration c = yourVector.elements(); c.hasMoreElements(); ){
yourClassType s = (yourClass) c.nextElement()
s.yourmethod/variable name
}
I hope this helps.


------------------
Bosun
SCJP for the Java� 2 Platform
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this thread to the Java Beginner forum. It doesn't belong in this one
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic