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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java - ArrayList - Quick Question

  • 08-01-2008 4: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, Registered Users 2 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, Registered Users 2 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