Java ArrayList Methods

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 69:-  Java ArrayList Methods

ArrayList in Java provides a wide range of methods to manipulate the list of elements it holds. Here are some of the commonly used methods of the ArrayList class:

  1. Adding Elements:
    • add(E element): Appends the specified element to the end of the list.
    • add(int index, E element): Inserts the specified element at the specified position in the list.
  2. Accessing Elements:
    • get(int index): Returns the element at the specified index.
    • set(int index, E element): Replaces the element at the specified index with the specified element.
  3. Removing Elements:
    • remove(int index): Removes the element at the specified index from the list.
    • remove(Object o): Removes the first occurrence of the specified element from the list.
    • clear(): Removes all elements from the list.
  4. Size and Checking Empty:
    • size(): Returns the number of elements in the list.
    • isEmpty(): Returns true if the list is empty, otherwise false.
  5. Searching Elements:
    • contains(Object o): Returns true if the list contains the specified element, otherwise false.
    • indexOf(Object o): Returns the index of the first occurrence of the specified element, or -1 if not found.
    • lastIndexOf(Object o): Returns the index of the last occurrence of the specified element, or -1 if not found.
  6. Sublist Operations:
    • subList(int fromIndex, int toIndex): Returns a new ArrayList containing elements from fromIndex (inclusive) to toIndex (exclusive).
  7. Converting to Array:
    • toArray(): Converts the ArrayList to a regular array of objects.
    • toArray(T[] a): Converts the ArrayList to an array of the specified type T.
  8. Iterating Over ArrayList:
    • You can use the enhanced for loop (for-each loop) or the traditional for loop to iterate over the elements in an ArrayList.

javaCopy code

import java.util.ArrayList; public class ArrayListMethodsExample {    public static void main(String[] args) {        ArrayList<Integer> numbers = new ArrayList<>();        // Adding elements        numbers.add(10);        numbers.add(20);        numbers.add(30);        // Accessing elements        int secondNumber = numbers.get(1); // 20        // Removing elements        numbers.remove(0); // Remove 10        // Size and checking empty        int size = numbers.size(); // 2        boolean isEmpty = numbers.isEmpty(); // false        // Searching elements        boolean contains30 = numbers.contains(30); // true        int index = numbers.indexOf(30); // 1        // Sublist operations        ArrayList<Integer> sublist = numbers.subList(0, 1); // [20]        // Converting to array        Integer[] numbersArray = numbers.toArray(new Integer[0]);    } }

These are just some of the many methods available in the ArrayList class. The Java ArrayList provides a rich set of operations to manage and manipulate collections of elements efficiently. For more details and additional methods, you can refer to the official Java documentation for the ArrayList class.

11. ArrayList

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?