Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove element from ArrayList without left shifting the rest of elements

Tags:

java

arraylist

When you remove an element from an array list (arrayList.remove(int n)) it automatically shifts all the remaining elements to the left.

I was wondering is it possible for the elements to keep their position. So for example, if element at position 2 gets deleted, I want element at position 3 to remain where it is, not move to position 2. Thanks

like image 608
Matt9Atkins Avatar asked Apr 30 '12 23:04

Matt9Atkins


1 Answers

list.set(2, null);

This just replaces the element at index 2 with null, which leads to the desired result.

like image 113
JB Nizet Avatar answered Oct 28 '22 13:10

JB Nizet