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

Java - ArrayList - Quick Question

Options
  • 08-01-2008 5:02pm
    #1
    Closed Accounts Posts: 12,382 ✭✭✭✭


    Hello

    I hope someone can help me with this.

    I have an ArrayList.

    I execute the code -

    myArrayList.add(x)
    myArrayList.add(y)

    - so x is added before y.

    What would myArrayList.get(0) retrieve? x or y?

    Basically I would like to be able to retrieve whichever element was added first without having to specify a direct position such as 0 or 1.

    Is this possible?

    Thank you. :)


Comments

  • Registered Users Posts: 610 ✭✭✭nialo


    it would retreive x.

    look at the api to find out more about arraylist
    http://java.sun.com/j2se/1.5.0/docs/api/

    myArrayList.get(myArrayList.indexOf(x));

    that would retrieve without specifing position. but requires you to know which element you added first!


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Yeah thats right, it will load the first element (position 0). IE first element received.


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Great, thanks Nialo and Webmonkey.


Advertisement