# Arraylist index question

• 02-27-2012, 04:23 PM
stuckonjava
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
• 02-27-2012, 04:41 PM
JosAH
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
• 02-27-2012, 04:56 PM
stuckonjava
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?
• 02-27-2012, 05:01 PM
JosAH
Re: Arraylist index question
Quote:

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
• 02-27-2012, 05:07 PM
stuckonjava
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>();
• 02-27-2012, 05:56 PM
JosAH
Re: Arraylist index question
Quote:

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
• 02-27-2012, 11:17 PM
stuckonjava
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
}
• 02-28-2012, 12:07 AM
stuckonjava
Re: Arraylist index question
anyone?
• 02-28-2012, 12:33 AM
stuckonjava
Re: Arraylist index question
• 02-28-2012, 12:47 AM
stuckonjava
Re: Arraylist index question
whats happened to everyone
• 02-28-2012, 01:35 AM
stuckonjava
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--;
}}
• 02-28-2012, 09:00 AM
JosAH
Re: Arraylist index question
Quote:

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