aspose file tools*
The moose likes Beginning Java and the fly likes Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vector" Watch "Vector" New topic
Author

Vector

Joey Alencar
Ranch Hand

Joined: Jun 26, 2001
Posts: 43
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

Joined: Nov 06, 2000
Posts: 1510
Can you elaborate? Do you mean how to manipulate instances of your class using a vector or what?

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Joey Alencar
Ranch Hand

Joined: Jun 26, 2001
Posts: 43
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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Jun 26, 2001
Posts: 43
OK, but how can I get the values of my class's variables using this Vector?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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

Joined: Aug 06, 2001
Posts: 88
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

Joined: Nov 04, 2001
Posts: 35
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


WebNelly.com<br />Java/XML Web Development<br />Check it out!<br /><a href="http://www.webnelly.com" target="_blank" rel="nofollow">http://www.webnelly.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector