i have an ArrayList with elements for example:
[a,b,c,d,e,f,g,h];
and need to take elements [a,b,c] also it should be an ArrayList and remove them from first List and again i take some elements from first list after i take i remove them.
So what is the best way (considering a time) to do this job ?
I mean i should iterate over elements or should convert to array and than work with it ?
maybe there are some fast way to do this ?
Thanks.
Create a new list using
subList()to get a view into the first, then useclear()on the subList. Example:This is much more efficient than iterating over the original list removing one element at a time since removing an element from an ArrayList involves shifting all the elements. Using a
subListdoes the shift just once.