Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Vectors and objects

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Vectors and objects

    Basicly it like this. I have a class full of methods and variables (like any other normal class) and I wanted to create a dynamic array of these instances. but iv'e come accross a floor in my plan where once I load the object of the class into the vector I can not access anything inside the class.

    Heres a more Codey kind of example...

    aClass obj = aClass();
     
    Vector x = new Vector();
    x.add(obj);
     
    //How can I accsess a method\function from inside, sorta like this
     
    x.get(0).someFunction();
    //where someFunction is a function defined inside aClass

    Thanx in advance, Matty


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,441
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Vectors and objects

    Use an ArrayList, and use Generics.

    AClass obj = AClass();
     
    ArrayList<AClass> x = new ArrayList<AClass>();
    x.add(obj);
     
    //this is now legal without casting,
    //because the ArrayList knows it holds AClass references
    x.get(0).someFunction();
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Vectors and objects

    Use the generic parameters of the vector.

    aClass obj = aClass();
     
    Vector<aClass> x = new Vector<aClass>(); // x can only hold aClass objects
    x.add(obj);
     
    x.get(0).someFunction();
    //where someFunction is a function defined inside aClass

    I would recommend using an ArrayList, btw. I can't really think of a place where Vector should be used instead of ArrayList (even in multi-threading applications).

    edit: darn, beat me to it kevinworkman

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Vectors and objects

    Thanx Guys i'll try that now

Similar Threads

  1. Need help with Vectors
    By Locky in forum Collections and Generics
    Replies: 6
    Last Post: October 19th, 2010, 04:45 PM
  2. Vectors
    By UnderX in forum Object Oriented Programming
    Replies: 1
    Last Post: October 19th, 2010, 04:29 PM
  3. Question regarding bit vectors...
    By ADizzle491 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 21st, 2010, 08:50 PM
  4. Vectors - accessing an unknown amount of objects
    By fox in forum Loops & Control Statements
    Replies: 1
    Last Post: May 7th, 2010, 03:54 PM
  5. Vectors
    By mgutierrez19 in forum Collections and Generics
    Replies: 4
    Last Post: March 3rd, 2010, 10:46 AM