Results 1 to 7 of 7
  1. #1
    Mnem is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default Ordered list - inserting wherever on the list

    Hi,
    I'm looking for and ordred list structure. The problem is that the lists I found I only could insert object at the begining or at the and of the list. I want to be able to insert objets at any place of the list because I want it to be ordered.
    does anybody knows how can I do it?
    Thanks in advance

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Have you checked ArrayList ?

    Specifically, ArrayList.add(int index, Object element)

    This inserts the specified element at the specified index position in this list.

    Does that satisfy your need?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Mnem is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks Goldest,
    I that's what I was looking for.
    Mnem

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    You are Welcome! :D
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    Mnem is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks Goldest,
    One more question about it.
    If we have a list with two objects and we want to insert an object between them. The existing two objects have the indexes 1 and 2 for exemple. We want the new object to be the one with the index 2 and the actual with the index 2 to have the index 3. If we execute: arraylist.add(2, new object), will it place the actual with index 2 to the third positiion or it will put the new object in place of the existing deleting the actual in the 2 position?.
    I would like it to insert the new object between the two existing, otherwise I would have to move the objects to free the second position and then insert the new object.
    Mnem

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    This is what javadoc has to say about this:
    "Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices)."

    Which means, if you insert a new object between index 1 and 2, the new object will sit at index 2 and old object at index 2 will move to index 3.

    Hope that's clear,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    Mnem is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  2. Replies: 2
    Last Post: 11-22-2009, 06:24 PM
  3. Replies: 3
    Last Post: 11-21-2009, 06:48 PM
  4. Now stuck with inserting data into a list.
    By mainy in forum New To Java
    Replies: 2
    Last Post: 07-31-2009, 04:49 PM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM

Posting Permissions

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