Accessing Object Variables of an Object, stored in a Vector


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Accessing Object Variables of an Object, stored in a Vector

  1. #1
    Join Date
    Oct 2004
    Posts
    2

    Accessing Object Variables of an Object, stored in a Vector

    Hi,

    How do I access the object variables of an object which is stored in a vector?

    e.g.

    public class students
    {
    pubilc String Name;
    public students(String aName)
    {
    this.Name = aName;
    }
    }


    // somewhere in the main method

    Vector allStudents = new Vector();

    aStudent = new Students("Sanjay");
    allStudents.add(aStudent);

    for (int k=0; k < allStudents.size(); k++)
    {
    Object m = allStudents.get(k);
    System.out.println(m.Name);
    }

    This is throwing up an error "cannot resolve symbol variable Name".

    Can anyone see where I am going wrong?

    Thanks in advance,
    Sanjay.

  2. #2
    Join Date
    Jun 2004
    Posts
    199
    System.out.println(((Students)m).Name);

    with Java 5.0 it can be done different. I've not moved to it so I don't know the syntax off the top off my head.

  3. #3
    Join Date
    Oct 2004
    Posts
    2
    That is is the correct solution!

    Thanks.
    Sanjay.

  4. #4
    Join Date
    Jun 2004
    Posts
    199
    Well sort of. It works for what you are doing. If you want to access more than one attribute or method, go ahead and cast it on the get from the Vector.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center