1. Senior Member
Join Date
Jan 2012
Posts
151
Rep Power
6

## 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. ## 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

3. Senior Member
Join Date
Jan 2012
Posts
151
Rep Power
6

## 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. ## Re: Arraylist index question

Originally Posted by stuckonjava
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

5. Senior Member
Join Date
Jan 2012
Posts
151
Rep Power
6

## 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. ## Re: Arraylist index question

Originally Posted by stuckonjava
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

7. Senior Member
Join Date
Jan 2012
Posts
151
Rep Power
6

## 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. Senior Member
Join Date
Jan 2012
Posts
151
Rep Power
6

## Re: Arraylist index question

anyone?

9. Senior Member
Join Date
Jan 2012
Posts
151
Rep Power
6

## Re: Arraylist index question

10. Senior Member
Join Date
Jan 2012
Posts
151
Rep Power
6

## Re: Arraylist index question

whats happened to everyone

11. Senior Member
Join Date
Jan 2012
Posts
151
Rep Power
6

## 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. ## Re: Arraylist index question

Originally Posted by stuckonjava
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

#### Posting Permissions

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