Results 1 to 2 of 2
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Rep Power

    Default Vectors - accessing an unknown amount of objects


    In basic form I have a class called students which make student objects with an instance variable called mark.

    Theres a question I have thats confusing me. It says that a method will take in a vector as its parameter of student objects. The requirements of the question are then for how many objects there are in the vector (The number of student objects in the vector will be random) that the mark of these students would be displayed. So for example if theres three student objects with marks 20,30,40. These values will be printed out like, "20 30 40"

    I understand that I can simply find out the size of the vector with int vecSize=vectorName.size();

    But what confuses me is after this how on earth will I be able to retrieve all of these objects as the size of the vector is unknown and number of objects in that vector are unknown?

    Any examples or explainations of the best way of doing this are much appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    You'll want to use a for loop, either the standard for loop going from 0 to vectorName.size() or else the foreach loop: for (Student student: vectorName) {...

    If the first standard for loop, use the Vector's get(int i) method to get the Student object corresponding to that index number. If the foreach loop, then each time it loops the student variable will hold the next Student in the Vector.

Similar Threads

  1. Accessing objects between classes
    By bikashg in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 12:05 PM
  2. Accessing objects from within Action listener
    By cog in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 08:17 PM
  3. Constructor with unknown amount of objects?
    By Bernard Robitaille in forum New To Java
    Replies: 5
    Last Post: 03-01-2009, 05:00 AM
  4. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 07:55 AM
  5. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 02:57 PM

Posting Permissions

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