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.

Share:
12,852
atom2ueki
Author by

atom2ueki

Updated on July 01, 2022

Comments

  • atom2ueki
    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? q1

  • atom2ueki
    atom2ueki over 10 years
    your method seems only remove the 20th element
  • atom2ueki
    atom2ueki over 10 years
    When 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
    micha over 10 years
    Have 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
    Katona over 10 years
    @atom2ueki do not use ArrayList as the type of the variable, use List as I did with list in my answer
  • atom2ueki
    atom2ueki over 10 years
    yap,that's return me an correct result, :),but what's different between list and arraylist here?
  • Katona
    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 of List 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
    atom2ueki over 10 years
    but 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
    mike over 10 years
    Maps 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
    mike over 10 years
    I think it would be easier to unterstand, if you loop backwards from 30 to 20. And use list.remove(i)
  • atom2ueki
    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
    blueware almost 9 years
    You made my day buddy, that's what I was looking for ;)