• 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: 1510
  • 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: 1510
  • 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
 
Colin Kenworthy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't having:
yourClassType s = (yourClass) c.nextElement();
within a for loop create a new variable 's' each time you pass through the loop?
Or is the compiler smart enough to spot this and re-use 's' ??
 
Kris Nelson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The complier doesn't exactly 're-use' the same s object. When the iteration of the for loop is complete (at the end of the '}' ), the s object is eligible for garbage collection because it goes out of scope. Once its ready for gc, it's up to the JVM to discard it.

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic