DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  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.

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links