Home arrow static arrow Java Programming [Archive] - Size of a Vector
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Size of a Vector
This topic has 5 replies on 1 page.

Posts:39
Registered: 7/19/04
Size of a Vector  
Aug 7, 2004 12:47 PM



 
Does any one know how to access the size of a Vector in one class from another class.
I am not getting the right syntax.

Thanks.
 

Posts:37,103
Registered: 3/30/99
Re: Size of a Vector  
Aug 7, 2004 12:54 PM (reply 1 of 5)



 
Huh?

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#size()

Can you explain your problem in a little more detail? "I have the wrong syntax" isn't very decriptive.
 

Posts:18,384
Registered: 21.03.00
Re: Size of a Vector  
Aug 7, 2004 12:56 PM (reply 2 of 5)



 
I agree with previous poster. .What do you mean? Show us the code you got so far. (And remember to post using the code tags (see formatting help))

/Kaj
 

Posts:37,103
Registered: 3/30/99
Re: Size of a Vector  
Aug 7, 2004 12:56 PM (reply 3 of 5)



 
If the vector is a member variable of one class, and you want another class to be able to find out its size, then you need to provide a method in the first class that calls Vector's size() method and returns its results.

As a very rough example...
public class Foo {    private Vector vec_ = new Vector();     public int size() {        return vec_.size();    }} public class OtherClass {    void bar(Foo foo) {        int fooSize = foo.size();    }} 
 

Posts:37,103
Registered: 3/30/99
Re: Size of a Vector  
Aug 7, 2004 12:58 PM (reply 4 of 5)



 
Of course, a good principle of OO is that you don't expose implementation details unless you have a reason to do so. Does the second class really have a reason to need the size from the first class?

That's a rather large topic in itself, so I'm not going to go into details here, but I'd be remiss if I provided a solution that exposes implementation details without at least mentioning that it's not always advisable to do so.
 

Posts:5,965
Registered: 5/17/03
Re: Size of a Vector  
Aug 7, 2004 1:21 PM (reply 5 of 5)



 
Does any one know how to access the size of a Vector
in one class from another class.

This is one way of many,
public class AnotherClass {   public Vector v = new Vector();}//public class OneClass {   private AnotherClass ac = new AnotherClass(); // instantiate object of AnotherClass   public void someMethod() {      int size = ac.v.size();  // size of Vector object in AnotherClass   }}
 
This topic has 5 replies on 1 page.