Results 1 to 4 of 4
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default Naming Variables

    Hi,

    At the moment I am trying to make a method which takes as a parameter a vector. Just would like to know say for example if the size of a vector is unknown. And in the method to find this out I would do:

    int vecSize= vectorName.size();

    To find the size of the vector. What i'd like to know is say I wanted to extract these objects in the vector. So for example say vector v has size 3.

    What is the best way to individually extract these?

    I was thinking of making a for loop:

    for(int i=0;i<vecSize;i++){

    And then each turn of the loop it creates a new object from casting with the elementAt(i); method

    But i'm unsure of how to extract these objects as if in the for loop body i done:

    Student stu = (Student) vec.elementAt(i); //I made a student class to create student objects

    For all loops only one student called stu is made and is overwritten after each loop i think.

    Is there anyway to create a new individual object each loop or is there a better way of doing this

    Thanks in advance

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Yes, you are right, its getting overwritten. So how about using a Student type array?
    Swastik

  3. #3
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    Hi, this is a question for college which must use the vector as a parameter. I was looking at ArrayLists in the loop, but I am confused on how to make the multiple student objects in the for loop? Or am I going in the wrong direction?

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    u can make Vector itself as an array using toArray() method
    Ramya:cool:

Similar Threads

  1. Variable Naming
    By 67726e in forum Advanced Java
    Replies: 2
    Last Post: 03-30-2010, 09:55 AM
  2. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 PM
  3. Naming a Class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 12:21 PM
  4. RMI naming problem
    By Robbinz in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 12:32 AM
  5. Naming conventions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:53 AM

Posting Permissions

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