How to remove elements in an arraylist start from an indicated index
12,852
Solution 1
You can use List#subList(int, int):
List<Integer> list = ...
list = list.subList(10, list.size()); // creates a new list from the old starting from the 10th element
or, since subList
creates a view on which every modification affects the original list, this may be even better:
List<Integer> list = ...
list.subList(0, 10).clear(); // clears the first 10 elements of list
Solution 2
Just use the remove() method for this.
Let's assume you want to remove elements with indices from 20 to 30 of an ArrayList
:
ArrayList<String> list = ...
for (int i = 0; i < 10; i++) { // 30 - 20 = 10
list.remove(20);
}
Once the first element at index 20
is removed element 21
moves to index 20
. So you have to delete 10 times the element at index 20
to remove the next 10 elements.
Author by
atom2ueki
Updated on July 01, 2022Comments
-
atom2ueki almost 2 years
As the picture shown, after one time run a method, i want remove the old items, and prepared for next time calculation, but i wondering how to remove elements in an arraylist start from an indicated index, like a queue, obey FIFO algorithm?
-
atom2ueki over 10 yearsyour method seems only remove the 20th element
-
atom2ueki over 10 yearsWhen i use "purchase=(ArrayList<Integer>) purchase.subList(index,purchase.size());" gives me an exception error..java.lang.ClassCastException: java.util.RandomAccessSubList cannot be cast to java.util.ArrayList
-
micha over 10 yearsHave you read the sentence bellow the code? There are no "holes" within an array list. If you remove the element with index 20 the next element will move to this index.
-
Katona over 10 years@atom2ueki do not use
ArrayList
as the type of the variable, useList
as I did withlist
in my answer -
atom2ueki over 10 yearsyap,that's return me an correct result, :),but what's different between list and arraylist here?
-
Katona over 10 years@atom2ueki
List
is an interface which defines the behaviour of a list without saying anything of the details.ArrayList
is an implementation ofList
which happens the store the elements in an array (this is a detail, which you are not facing it when you use the interface). Read more here. -
atom2ueki over 10 yearsbut if using map,i still need an identifier for each map,does it the same of list here? sorry, i'm not fully get what you mean
-
mike over 10 yearsMaps don't work with indices, they work with Keys. Keys are objects, that's what one would do in OOP, it's easier to maintain the code with identifiers than maintaining codes where integers have a meaning beyond just being a number. You should write an
Item
class or sth. like that and use it as key to a map. -
mike over 10 yearsI think it would be easier to unterstand, if you loop backwards from 30 to 20. And use
list.remove(i)
-
atom2ueki over 10 years@micha i tried tis method, but seems that loop is not started, the logic is correct, it only remove one element 20
-
blueware almost 9 yearsYou made my day buddy, that's what I was looking for ;)