Results 1 to 3 of 3

Thread: on vectors

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    on vectors

    first of all, thanks to all who've helped me thus far... i really appreciate it.

    1. how do i determine the number of elements in a vector?
    i tried:

    i = vectorName.elementCount();

    but it doesnt work.

    2. if the vector stores user-defined objects (and these objects consist of int, string, date etc), can i access a particular attribute of an object in the vector directly? i.e.

    int retrieved;

    retrieved = vectorName.elementAt(0).age; //where age is of int type

  2. #2
    Lively Member
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    115
    1.
    i = vectorName.size();

    2.
    Sure you can
    retrieved = ((ClassOfStoredObject)vectorName.elementAt(0)).age;
    should be working (haven't tried it )

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
     import java.util.Vector; 
     
     class X{
      private String greeting;
    
      public X(String greeting){
       this.greeting = greeting; 
      } 
      public String getGreeting(){
       return greeting; 
      }  
    }
    
    public class Z {
      public static void main(String[] args){
       Z z = new Z(); 
       z.invoke(); 
     }
    
      private X x; 
      private Vector v = new Vector(); 
    
      private void invoke(){ 
       v.addElement(new X("Hello"));
       x = (X) v.elementAt(0);
       System.out.println(x.getGreeting()); 
     } 
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width