Remove Element from the specific index in ArrayList

In this tutorial we will learn  ,how to remove element from specific in Arraylist . To remove index specific in Arraylist we used method remove(int index) to remove element from Arraylist .
It throws IndexOutOfBoundException if  the specific index is less than zero or greater than size of the list  .

See Example:

import java.util.ArrayList;

public class Example {

public static void main(String[] args) {
  ArrayList<String> arrlist  = getArrayList();

}
public static ArrayList<String> getArrayList(){
ArrayList<String> arrlist = new ArrayList<String>();
//add method for String ArrayList
 arrlist.add("Item1");
 arrlist.add("Item2");
 arrlist.add("Item3");
 arrlist.add("Item4");
 arrlist.add("Item5");
 System.out.println("ArrayList Items ");
 for(String arr : arrlist){
  System.out.println(arr);
 }
 //remove(item3 of second index of Arraylist ) ,specific index(2)
 arrlist.remove(2);
 System.out.println("ArrayList after remove Item ");
 for(String arr : arrlist){
  System.out.println(arr);
 }
 
  return arrlist;
}
}

//result

ArrayList Items
Item1
Item2
Item3
Item4
Item5
ArrayList after remove Item
Item1
Item2
Item4
Item5

after removed we saw "Item3" lost from Arraylist when remove specific index 

Related Posts


EmoticonEmoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng
:lv