Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Vector of records in Java.

Options
  • 30-04-2003 10:07pm
    #1
    Registered Users Posts: 6,315 ✭✭✭


    Hi,
    I've never used a Vector before. I'm trying to change a project from being an array to being one based on an array (our latest project requires us to make changes to the last).

    I've declared the Vector::

    Vector recordSet;

    Then I instanitated it::

    recordSet = new Vector();

    I then tried to this an a few other things and it throws an error::

    public void add(Student newRecord)
    {
    recordSet.elementAt(count++) = newRecord;
    }

    The error is::

    variable required but value found.

    Any help much appreciated.


Comments

  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    Read the API for Vector .. you probably want to be doing something like
    recordSet.add( newRecord );
    
    in your function. You're accessing a Vector as if it were an array, which its not.

    .cg


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    go to

    http://forum.java.sun.com : at the bottom right corner of the screen, enter the search topic

    if you look for array to vector you will find a wealth of info. I know cos I had to do something similar....


  • Registered Users Posts: 1,186 ✭✭✭davej


    For a start you should probably think about using an ArrayList instead of a Vector.

    Look at what the doc. for Vector and see what the elementAt mehod returns: an Object.
    However you are trying to assign this to a Class called Student. Assuming you put a Student object into the Vector in the first place, you need to cast the returned object as a Student:

    (Student) recordSet.elementAt(count++) = newRecord;

    Looking at your code again I also see that you appear to have completely mistaken the use of this method, you use the add method to add an object to the Vector:

    add(int index, Object element)
    Inserts the specified element at the specified position in this Vector.

    or

    add(Object o)
    Appends the specified element to the end of this Vector.

    davej


  • Registered Users Posts: 6,315 ✭✭✭ballooba


    Sorry lads,
    Got it finisherd late last night, was to tired to write back. The answer was ::

    recordSet.elementAt(count++) = newRecord;

    Should have been ::

    recordSet.addElement(newRecord);

    Thanks for the help anyway. Sorry to have wasted your time.

    I had forgtten to change that anyway so that wasn't the problem that was a mistake::

    The real problem was::
    currentRecord = recordSet.elementAt(count++);

    should be::
    currentRecord = (Student)recordSet.elementAt(count++);


Advertisement