bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java ArrayList remove() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java ArrayList remove() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ java.util.ArrayList;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The remove() method removes an item from the list, either by position or by value.
Definition and Usage
Java ArrayList remove() Method

❮ ArrayList Methods

Example

import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    cars.remove(0);
    System.out.println(cars);
  }
}

Definition and Usage

The remove() method removes an item from the list, either by position or by value. If a position is specified then this method returns the removed item. If a value is specified then it returns true if the value was found and false otherwise.

If a value is specified and multiple elements in the list have the same value then only the first one is deleted.

If the list contains integers and you want to delete an integer based on its value you will need to pass an Integer object. See More Examples below for an example.

One of the following

public T remove(int
index )
public boolean remove(Object
item )

T refers to the data type of items in the list.

Parameter Values

ParameterDescription
indexRequired. The position of the item to be deleted.
itemRequired. The value of the item to be deleted.

Technical Details

Returns:If an object was passed as an argument then it returns true if the object was found in the list and false otherwise. If an index was passed then it returns the object which was removed.
Throws:IndexOutOfBoundsException - If the index is less than zero, equal to the size of the list or greater than the size of the list.

Previous

Java ArrayList listIterator() Method

Next

Java ArrayList removeAll() Method