Search code examples
javaandroidarraylistint

How to remove value from integer arraylist.ANDROID


I created an arraylist for Integer values.

 List<Integer> xpos = new ArrayList<Integer>();

adding the integers to the arraylist as follows

 int bt_x=10; 
 xpos.add(bt_x);

Now how to remove single value ie. how to remove an integer from the arraylist. We can remove string arraylist with arraylist.get(i).remove. but how to remove the integer arraylist.


Solution

  • Use ArrayList.remove(java.lang.Object) method. like

    xpos.remove(Integer.valueOf( premitive int value ));
    

    if you want to remove bt_x then use

    xpos.remove(Integer.valueOf(bt_x));
    

    Do not forget to cast int to Integer. If you do not do, it will remove the element at given value.


    And yes, Properly removing an Integer from a List<Integer> should be read.