public static < T > void fastUnorderedRemove ( final ArrayList < T > list , final int index ) { final int lastIndex = list . size ( ) - 1 ; if ( index != lastIndex ) { list . set ( index , list . remove ( lastIndex ) ) ; } else { list . remove ( index ) ; } }
Removes element at index but instead of copying all elements to the left moves into the same slot the last element . This avoids the copy costs but spoils the list order . If index is the last element it is just removed .