Java Arraylist Remove
[ Java ArrayList](#)
The remove() method is used to remove a single element from a dynamic array.
The syntax of the remove() method is:
// Remove the specified element
arraylist.remove(Object obj)
// Remove the element at the specified index
arraylist.remove(int index)
**Note:** arraylist is an object of the ArrayList class.
**Parameter Description:**
* obj - The element to be removed
* index - The index of the element to be removed
If obj appears multiple times, the first occurrence in the dynamic array is deleted.
### Return Value
If an element is passed and deleted successfully, it returns true.
If an index is passed, it returns the removed element.
Note: If the specified index is out of range, the method will throw an IndexOutOfBoundsException.
### Example
Remove the specified element from the array:
## Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// Create an array
ArrayList sites =new ArrayList();
sites.add("Google");
sites.add("Tutorial");
sites.add("Taobao");
System.out.println("Website List: "+ sites);
// Remove element Taobao
boolean result = sites.remove("Taobao");
System.out.println("Taoabo Whether removed? "+ result);
System.out.println("Using remove() Post: "+ sites);
}
}
The output of running the above program is:
Website List: [Google, Tutorial, Taobao]
Taoabo Whether removed? true
Using remove() Post: [Google, Tutorial]
In the example above, we used the remove() method to delete the element Taobao from the dynamic array.
Remove element at specified position:
## Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// Create an array
ArrayList sites =new ArrayList();
sites.add("Google");
sites.add("Tutorial");
sites.add("Taobao");
System.out.println("Website List: "+ sites);
// Remove element at index 2
String element = sites.remove(2);
System.out.println("Using remove() Post: "+ sites);
System.out.println("Removed element: "+ element);
}
}
The output of running the above program is:
Website List: [Google, Tutorial, Taobao]
Using remove() Post: [Google, Tutorial]
Removed element: Taobao
Remove the first occurrence of an element:
## Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// Create an array
ArrayList randomNumbers =new ArrayList();
// Insert elements into the array
randomNumbers.add(22);
randomNumbers.add(13);
randomNumbers.add(35);
randomNumbers.add(13);
randomNumbers.add(40);
System.out.println("ArrayList: "+ randomNumbers);
// Remove the first occurrence of 13
boolean result = randomNumbers.remove(Integer.valueOf(13));
System.out.println("13 Whether removed? "+ result);
System.out.println("Using remove() Post: "+ randomNumbers);
}
}
The output of running the above program is:
ArrayList: [22, 13, 35, 13, 40]
13 Whether removed? true
Using remove() Post: [22, 35, 13, 40]
In the example above, we created a dynamic array named randomNumbers. In this array, the element 13 appears twice. Note this line:
randomNumbers.remove(Integer.valueOf(13))
Integer.valueOf() converts 13 from int type to an Integer object. This is because the remove() method only takes an object as its parameter.
remove() is used to delete the first occurrence of 13 in the array.
Note: We can also use the clear() method to remove all elements in the array. To learn more, visit [Java ArrayList clear()](#).
[ Java ArrayList](#)
YouTip