In reguards to the remove function, what happens if you remove and item from the middle of this list? For example, say the list size was 5 (elements 0 - 4) and element 3 was removed. Would element 3 now contain a null value or would everything past element 3 get shifted to the left, or is it treated like a linked list and the refereance is just broken and redirected to element 4. Just curious, every example i've seen always removes from the end of the list.

Thanks

Recommended Answers

All 9 Replies

what happens if

Sounds like an easy program to write and test what happens. And probably faster than waiting for someone to come along.

Could you write a program to test this and report back the results?

yeah that was my original idea, but i forgot to grab my laptop before i headed to work. I'll test it when I get home and post the result unless someone else beats me to it.

It does 'shrink' the list down. Well since it is a list it would probably be better to say that the pointers are rearranged. Anyway, I took an element out to the middle of the list and then tried to spit out the value of the last element of this list. This promptly threw an out of bounds exception.

Are you sure it was the position of the new last or the old last element?

Post the code if you have further questions.

Are you sure it was the position of the new last or the old last element?

Post the code if you have further questions.

It was the old last. I set the list up to have 5 elements ( 0 - 4). I removed element 3 then tried to output element 4. That is when I got the out of bounds exception. I then tried the same process again but instead of outputting element 4, I tried element 3. It then spit out the value I formally had in element 4.

Can you post your code?

Can you post your code?

import java.util.ArrayList;
public class test{
	
	public static void main (String args [])
	{
		ArrayList <String> bob = new ArrayList <String> ();
		bob.add ("1");
		bob.add ("2");
		bob.add ("3");
		bob.add ("4"); // right now is in element 3
		bob.remove(2); // this causes "4" to go to position 2 
		String taco = bob.get(3); // throws exception
		System.out.println(taco);
	}
}

however if you change bob.get(3) to bob.get(2) the output does become 4.

After removing 1 element from a list with 4 elements, how many elements are left?
Remember arrays are 0 based.

After removing 1 element from a list with 4 elements, how many elements are left?
Remember arrays are 0 based.

I get what is going on, now that I think about it my original question was kind of stupid to begin with. I mean what is an Arraylist... its a list with the referencing element numbers of an array. It should have been obvious if one removes an object from the list the elements would have to shift. Otherwise the nature of the linked list would be compromised, in other words you wouldn't be able to access any of the elements past the removed one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.