Results 1 to 12 of 12
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Arraylist index question

    Can someone explain how I would make a method for an arrayindexlist - removerange(int n, int k) where n is the from index (inclusive) and k is the to index(exclusive).

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylist index question

    Remove the elements from the end, i.e. remove element k-1, k-2 ... n, starting at element k-1.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Arraylist index question

    I don't understand,

    If we have the array with 3,8,6,9,19 and we want to remove 6 and 9 that would be removeRange(3,5) so the new array is 3,8,19.

    how do i do this so that the 3rd and 4th index are removed and the 19 is brought to index 2?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylist index question

    Quote Originally Posted by stuckonjava View Post
    I don't understand,

    If we have the array with 3,8,6,9,19 and we want to remove 6 and 9 that would be removeRange(3,5) so the new array is 3,8,19.

    how do i do this so that the 3rd and 4th index are removed and the 19 is brought to index 2?
    Carefully reread my previous reply: first remove element #4, next remove element #3. If you're using simple arrays you have to fill the gap by moving all elements starting at position 5, (5-3) 2 elements to the left.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Arraylist index question

    How would i do it for an IndexList, what is the difference in the method between that and the normal one.

    ArrayIndexList<Integer> a = new ArrayIndexList<Integer>();

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylist index question

    Quote Originally Posted by stuckonjava View Post
    How would i do it for an IndexList, what is the difference in the method between that and the normal one.

    ArrayIndexList<Integer> a = new ArrayIndexList<Integer>();
    You don't have to fill a gap then, i.e. simply remove the elements from back to front. b.t.w. there is no ArrayIndexList class in the core set.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Arraylist index question

    public E remove(int r){
    throws IndexOutOfBoundsException{
    E temp = A[r];
    for (int i =r; i<size-1; i++)
    A[i] = A[i+1]
    size--
    return temp

    Could you tell me whether this is correct for removing a single element?

    Thanks
    }

  8. #8
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Arraylist index question

    anyone?

  9. #9
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Arraylist index question

    please

  10. #10
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Arraylist index question

    whats happened to everyone

  11. #11
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Arraylist index question

    can someone confirm whether this would work please:

    public E removeRange(int from, int to)
    throws IndexOutOfBoundsException{

    for(int i = to - 1; i >= from; i--)
    {
    A[i] = A[i + 1];
    size--;
    }}

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylist index question

    Quote Originally Posted by stuckonjava View Post
    public E remove(int r){
    throws IndexOutOfBoundsException{
    E temp = A[r];
    for (int i =r; i<size-1; i++)
    A[i] = A[i+1]
    size--
    return temp

    Could you tell me whether this is correct for removing a single element?

    Thanks
    }
    What did your compiler say about it? As far as I can see your code doesn't compile. Also, is 'size' a member variable in your own class? Is 'E' the type of the elements in your array 'A'? Besides all that, your code makes sense to me (logically). Test it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Adding an item to arrayList at a specific index
    By randoms:) in forum New To Java
    Replies: 2
    Last Post: 05-11-2011, 04:25 PM
  2. Java newbie question on linkedlist index search
    By Mjall in forum New To Java
    Replies: 5
    Last Post: 03-25-2011, 10:54 PM
  3. Find the index number of strings in an arraylist
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 03-24-2011, 03:25 PM
  4. Replies: 12
    Last Post: 11-05-2009, 07:12 AM
  5. Question on JTabbedPane.setComponentAt(index,component)
    By viswanadh7 in forum AWT / Swing
    Replies: 3
    Last Post: 03-13-2009, 09:36 AM

Posting Permissions

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