Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Vector of records in Java.

  • 30-04-2003 10:07PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 3,890 ✭✭✭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, Registered Users 2 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, Registered Users 2 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