Arraylist methods in java with examples

Christopher 75 Published: 10/29/2024

Arraylist methods in java with examples

I'll respond in English this time!

Arraylist is a part of the Java Collections Framework that provides a dynamic array data structure to store elements. ArrayList in Java is a resizable-array implementation of the List interface. Here are some common methods used in Arraylist along with examples:

add(E e): Adds the specified element at the end of this list.

Example:

ArrayList arrayList = new ArrayList<>();

arrayList.add("Apple");

arrayList.add("Banana");

arrayList.add("Cherry");

// Output: [Apple, Banana, Cherry]

addAll(Collection<? extends E> c): Inserts all of the elements in the specified collection into this list at the end.

Example:

ArrayList arrayList = new ArrayList<>();

ArrayList fruits = new ArrayList<>();

fruits.add("Mango");

fruits.add("Pineapple");

arrayList.addAll(fruits);

// Output: [Apple, Banana, Cherry, Mango, Pineapple]

clear(): Removes all of the elements from this list.

Example:

ArrayList arrayList = new ArrayList<>();

arrayList.add("Apple");

arrayList.add("Banana");

arrayList.clear();

// Output: []

contains(Object o): Returns true if this list contains the specified element.

Example:

ArrayList arrayList = new ArrayList<>();

arrayList.add("Apple");

arrayList.add("Banana");

System.out.println(arrayList.contains("Apple")); // True

System.out.println(arrayList.contains("Cherry")); // False

get(int index): Returns the element at the specified position in this list.

Example:

ArrayList arrayList = new ArrayList<>();

arrayList.add("Apple");

arrayList.add("Banana");

System.out.println(arrayList.get(0)); // Apple

System.out.println(arrayList.get(1)); // Banana

indexOf(Object o): Returns the index of the first occurrence of the specified element in this list.

Example:

ArrayList arrayList = new ArrayList<>();

arrayList.add("Apple");

arrayList.add("Banana");

System.out.println(arrayList.indexOf("Apple")); // 0

System.out.println(arrayList.indexOf("Cherry")); // -1 (not found)

remove(int index): Removes the element at the specified position in this list.

Example:

ArrayList arrayList = new ArrayList<>();

arrayList.add("Apple");

arrayList.add("Banana");

arrayList.remove(0);

// Output: [Banana]

set(int index, E e): Replaces the element at the specified position in this list with the specified element.

Example:

ArrayList arrayList = new ArrayList<>();

arrayList.add("Apple");

arrayList.add("Banana");

arrayList.set(0, "Cherry");

// Output: [Cherry, Banana]

size(): Returns the number of elements in this list.

Example:

ArrayList arrayList = new ArrayList<>();

arrayList.add("Apple");

arrayList.add("Banana");

System.out.println(arrayList.size()); // 2

toArray(): Returns an array containing all the elements in this list.

Example:

ArrayList arrayList = new ArrayList<>();

arrayList.add("Apple");

arrayList.add("Banana");

String[] fruitArray = arrayList.toArray(new String[0]);

// Output: [Apple, Banana]

These are some of the commonly used methods in Java's Arraylist class. There are many more methods available for manipulation and querying of the list.

Arraylist methods in java w3schools

I apologize for the misunderstanding earlier! I'll respond in English as you requested.

ArrayList is a class of Java that implements the List interface, which means it can store multiple values (known as elements) and can be manipulated using various methods. Here are some common ArrayList methods in Java:

add(E e): Adds a new element to the end of this list.

Example:

ArrayList myList = new ArrayList<>();

myList.add("Hello");

get(int index): Returns the element at the specified position in this list.

Example:

String myElement = myList.get(0); // returns "Hello"

size(): Returns the number of elements in this list.

Example:

int myListSize = myList.size(); // returns 1

isEmpty(): Returns true if this list contains no elements.

Example:

boolean isEmptyList = myList.isEmpty(); // returns true

myList.add("World");

isEmptyList = myList.isEmpty(); // returns false

set(int index, E element): Replaces the element at the specified position in this list with the specified element.

Example:

myList.set(0, "Goodbye");

System.out.println(myList.get(0)); // prints "Goodbye"

remove(int index): Removes the element at the specified position in this list.

Example:

myList.remove(0);

System.out.println(myList.size()); // returns 0

clear(): Removes all elements from this list.

Example:

myList.clear();

System.out.println(myList.size()); // returns 0

contains(Object o): Returns true if this list contains the specified element.

Example:

boolean myListContains = myList.contains("World"); // returns false

myList.add("World");

myListContains = myList.contains("World"); // returns true

indexOf(Object o): Returns the index of the first occurrence of the specified element, or -1 if it is not found.

Example:

int indexOfWorld = myList.indexOf("World"); // returns 0

lastIndexOf(Object o): Returns the index of the last occurrence of the specified element, or -1 if it is not found.

Example:

int lastIndexOfWorld = myList.lastIndexOf("World"); // returns 0 (assuming only one "World" in the list)

listIterator(): Returns a list iterator over the elements in this list (in proper sequence).

Example:

ListIterator myIterator = myList.listIterator();

while (myIterator.hasNext()) {

System.out.println(myIterator.next());

}

listIterator(int index): Returns a list iterator over the element(s) in this list starting at the given index.

Example:

ListIterator myIterator = myList.listIterator(1);

while (myIterator.hasNext()) {

System.out.println(myIterator.next());

}

iterator(): Returns an iterator over the elements in this list (in proper sequence).

Example:

Iterator myIterator = myList.iterator();

while (myIterator.hasNext()) {

System.out.println(myIterator.next());

}

These are some of the most commonly used ArrayList methods in Java, provided by W3Schools.