Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Java Arraylist Methods in Java

Java Arraylist Methods in Java

Here’s a comprehensive list of commonly used methods in ArrayList in Java. These methods allow you to add, remove, and manipulate elements within an ArrayList.

1. add(E e)

  • Description: Adds the specified element to the end of the list.

  • Example:

    ArrayList<String> fruits = new ArrayList<>();fruits.add("Apple");fruits.add("Banana");

2. add(int index, E element)

  • Description: Inserts the specified element at the specified index in the list.

  • Example:

    fruits.add(1, "Mango");  // Adds Mango at index 1

3. get(int index)

  • Description: Returns the element at the specified index.

  • Example:

    String fruit = fruits.get(0);  // Returns "Apple"

4. remove(int index)

  • Description: Removes the element at the specified index.

  • Example:

    fruits.remove(1);  // Removes the element at index 1 (Banana)

5. remove(Object o)

  • Description: Removes the first occurrence of the specified element from the list.

  • Example:

    fruits.remove("Apple");  // Removes "Apple" from the list

6. set(int index, E element)

  • Description: Replaces the element at the specified index with the specified element.

  • Example:

    fruits.set(0, "Grapes");  // Replaces the element at index 0 with "Grapes"

7. size()

  • Description: Returns the number of elements in the list.

  • Example:

    int size = fruits.size();  // Returns 2 if two elements are in the list

8. isEmpty()

  • Description: Checks if the list is empty.

  • Example:

    boolean empty = fruits.isEmpty();  // Returns false if the list has elements

9. contains(Object o)

  • Description: Checks if the list contains the specified element.

  • Example:

    boolean hasApple = fruits.contains("Apple");  // Returns false if "Apple" is not in the list

10. clear()

  • Description: Removes all elements from the list.

  • Example:

    fruits.clear();  // Removes all elements from the list

11. indexOf(Object o)

  • Description: Returns the index of the first occurrence of the specified element, or -1 if the list does not contain the element.

  • Example:

    int index = fruits.indexOf("Banana");  // Returns index of "Banana"

12. lastIndexOf(Object o)

  • Description: Returns the index of the last occurrence of the specified element, or -1 if the list does not contain the element.

  • Example:

    int lastIndex = fruits.lastIndexOf("Banana");  // Returns the last index of "Banana"

13. toArray()

  • Description: Converts the ArrayList to an array.

  • Example:

    Object[] fruitArray = fruits.toArray();  // Converts the list to an Object array

14. toArray(T[] array)

  • Description: Converts the ArrayList to an array of the specified type.

  • Example:

    String[] fruitArray = fruits.toArray(new String[0]);  // Converts list to String array

15. clone()

  • Description: Returns a shallow copy of the ArrayList.

  • Example:

    ArrayList<String> clonedList = (ArrayList<String>) fruits.clone();  // Creates a copy of the list

16. forEach(Consumer<? super E> action)

  • Description: Performs the given action for each element of the list (Java 8+).

  • Example:

    fruits.forEach(fruit -> System.out.println(fruit));  // Prints each element in the list

17. sort(Comparator<? super E> c)

  • Description: Sorts the elements in the list according to the order induced by the specified comparator.

  • Example:

    fruits.sort(Comparator.naturalOrder());  // Sorts the list in natural order

18. subList(int fromIndex, int toIndex)

  • Description: Returns a view of the portion of the list between the specified fromIndex and toIndex.

  • Example:

    List<String> subList = fruits.subList(0, 2);  // Returns a sublist from index 0 to 2

19. removeAll(Collection<?> c)

  • Description: Removes from the list all elements that are contained in the specified collection.

  • Example:

    ArrayList<String> removeList = new ArrayList<>();removeList.add("Apple");fruits.removeAll(removeList);  // Removes "Apple" from the list

20. retainAll(Collection<?> c)

  • Description: Retains only the elements in the list that are contained in the specified collection.

  • Example:

    ArrayList<String> retainList = new ArrayList<>();retainList.add("Banana");fruits.retainAll(retainList);  // Keeps only "Banana" in the list

Performance Considerations:

  • Random Access: ArrayList allows fast access to elements using the get() method, making it ideal for scenarios where you need to retrieve elements quickly by index.

  • Insertions/Deletions: While adding elements to the end of the list is generally efficient, removing elements or inserting them at the beginning or in the middle of the list can be slower due to the need to shift elements.


Example Program: Using ArrayList Methods

import java.util.ArrayList;public class ArrayListMethodsExample {    public static void main(String[] args) {        // Create an ArrayList        ArrayList<String> fruits = new ArrayList<>();                // Adding elements        fruits.add("Apple");        fruits.add("Banana");        fruits.add("Cherry");        // Display the ArrayList        System.out.println("Original List: " + fruits);        // Get an element        String fruit = fruits.get(0);        System.out.println("First element: " + fruit);        // Remove an element by index        fruits.remove(1);        System.out.println("List after removing index 1: " + fruits);        // Check if an element exists        boolean containsApple = fruits.contains("Apple");        System.out.println("Contains Apple: " + containsApple);        // Set a new value        fruits.set(0, "Grapes");        System.out.println("List after updating index 0: " + fruits);        // Size of the ArrayList        int size = fruits.size();        System.out.println("Size of the list: " + size);        // Clear the list        fruits.clear();        System.out.println("List after clearing: " + fruits);    }}

Output:

Original List: [Apple, Banana, Cherry]First element: AppleList after removing index 1: [Apple, Cherry]Contains Apple: trueList after updating index 0: [Grapes, Cherry]Size of the list: 2List after clearing: []

Summary Table of ArrayList Methods:

MethodDescription
add(E e)Adds element to the list
add(int index, E element)Adds element at specified index
get(int index)Retrieves element at index
remove(int index)Removes element at index
remove(Object o)Removes the first occurrence of the element
set(int index, E element)Replaces element at index
size()Returns the size of the list
isEmpty()Checks if the list is empty
contains(Object o)Checks if the list contains element
clear()Removes all elements
indexOf(Object o)Returns the index of the element
lastIndexOf(Object o)Returns the last index of the element
toArray()Converts to an array (Object array)
toArray(T[] array)Converts to an array of specified type
clone()Creates a shallow copy of the list
forEach(Consumer<? super E> action)Iterates through elements using action
sort(Comparator<? super E> c)Sorts the list
subList(int fromIndex, int toIndex)Returns a sublist from given range
removeAll(Collection<?> c)Removes all elements in the specified collection
retainAll(Collection<?> c)Retains only elements in the specified collection

Let me know if you'd like to explore more advanced usage or specific scenarios with ArrayLists! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql