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 2 of 2

Thread: Vectors - accessing an unknown amount of objects

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Vectors - accessing an unknown amount of objects

    Hi all

    First time on here, I'm pretty new to java. For college just been learning about vectors and such. I pretty much understand how to store objects in vectors and how to cast them to retrieve values 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
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Vectors - accessing an unknown amount of objects

    There are two simple ways:

    1. Use an enhanced for loop. This is probably the simpler of the two, but in my opinion limits what you can do concerning the main vector you're dealing with.

    for(Student s : students)
    {
       // do stuff with s
       System.out.println("Student's name: " + s.getName());
    }

    The above code can be read as "for each Student s in students, ... (stuff inside the for-each block)

    2. The second method is to use a standard for-loop (or a while loop) with a loop counter

    for (int i = 0; i < students.size(); i++)
    {
         System.out.println("Student " + i + "'s name: " + students.get(i));
    }

    As you can see, there are certain things you can do using a loop counter since you do have more information. However, you will need to keep track of the loop counter yourself (not a big deal if it's something simple like this, but can get hairy as you nest multiple loops)

Similar Threads

  1. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 06:11 PM
  2. [URGENT] - Problem accessing web sites with Java!
    By jguilhermemv in forum Java Networking
    Replies: 0
    Last Post: March 5th, 2010, 04:43 PM
  3. Vectors
    By mgutierrez19 in forum Collections and Generics
    Replies: 4
    Last Post: March 3rd, 2010, 11:46 AM
  4. [SOLVED] Web portal accessing files on the user's system via the Java I/O stream?
    By rendy.dev in forum Web Frameworks
    Replies: 2
    Last Post: January 18th, 2010, 08:46 PM
  5. Replies: 3
    Last Post: November 15th, 2008, 07:17 AM