ArrayList Methods in Java

In Java, an ArrayList is a class that provides a way to create resizable arrays. It is a part of the Java Collections framework and implements the List interface. Java ArrayList provides several methods for adding, removing, and accessing elements in the list

An ArrayList in Java is similar to a traditional array in that it stores elements of the same data type in a contiguous block of memory. However, unlike traditional arrays, an ArrayList can dynamically resize itself to accommodate any number of elements. This means that an ArrayList can grow or shrink in size as needed, making it more flexible than a traditional array.

To use an ArrayList in Java, you need to first create an instance of the ArrayList class and specify the data type of the elements that will be stored in the ArrayList. For example, to create an ArrayList of integers, you can use the following code:

For example:

ArrayList<Integer> myArrayList = new ArrayList<Integer>();

You can then add elements to the ArrayList using the add() method, retrieve elements using the get() method, and remove elements using the remove() method. The ArrayList class provides many other methods as well, including methods to check the size of the ArrayList. Check if it contains a particular element, and more.

How to declare an arrayList in java.

To declare an ArrayList in Java, you can use the following syntax:

ArrayList<DataType> arrayListName = new ArrayList<DataType>();

Here, DataType represents the data type of the elements that you want to store in the ArrayList, and arrayListName represents the name of the ArrayList.

For example, to declare an ArrayList of integers, you can use the following code:

ArrayList<Integer> numbers = new ArrayList<Integer>();

To add elements to the ArrayList, you can use the add() method. For example, to add the integer 5 to the numbers ArrayList, you can use the following code:

numbers.add(5);

You can add as many elements as you want to the ArrayList using the add() method.

To retrieve an element from the ArrayList, you can use the get() method. For example, to retrieve the first element of the numbers ArrayList, you can use the following code:

int firstNumber = numbers.get(0);

This will retrieve the first element (index 0) of the numbers ArrayList and store it in the firstNumber variable.

Note that the index of the first element in an ArrayList is 0, so to retrieve the nth element, you would use get(n-1).

Constructors in ArrayList

In Java, the ArrayList class is used to create resizable arrays that can be dynamically modified. Constructors are special methods that are used to create objects of a class. The ArrayList class provides several constructors to create ArrayList objects.

Here are the constructors available in the ArrayList class:

  1. ArrayList(): This constructor creates an empty ArrayList with an initial capacity of 10.
  2. ArrayList(int initialCapacity): This constructor creates an empty ArrayList with the specified initial capacity.
  3. ArrayList(Collection<? extends E> c): This constructor creates an ArrayList that contains the elements of the specified collection, in the order they are returned by the collection’s iterator.

For example, to create an ArrayList of strings using the default constructor, you can do the following:

ArrayList<String> myArrayList = new ArrayList<String>();

To create an ArrayList of integers with an initial capacity of 20, you can do the following:

ArrayList<Integer> myArrayList = new ArrayList<Integer>(20);

For create an ArrayList of characters containing the elements of an existing collection, you can do the following:

Collection<Character> myCollection = new HashSet<Character>();
myCollection.add('a');
myCollection.add('b');
myCollection.add('c');
ArrayList<Character> myArrayList = new ArrayList<Character>(myCollection);

To create an ArrayList of characters containing the elements of an existing collection, you can do the following:

Collection<Character> myCollection = new HashSet<Character>();
myCollection.add('a');
myCollection.add('b');
myCollection.add('c');
ArrayList<Character> myArrayList = new ArrayList<Character>(myCollection);

Note that in the last example, we first created a HashSet of characters and added some elements to it. Then we passed this HashSet to the ArrayList constructor to create a new ArrayList containing the same elements.

Explanation of each of the ArrayList constructors:

1.ArrayList():

ArrayList() constructor creates an empty ArrayList with an initial capacity of 10. The initial capacity is the number of elements that the ArrayList can store without needing to resize its internal array. If you add more elements than the initial capacity, the ArrayList will automatically resize its internal array to accommodate the additional elements. The default capacity of 10 is usually sufficient for most use cases, but if you know that you will be storing a large number of elements, you can use the second constructor to specify a larger initial capacity.

For Example:

This constructor creates an empty ArrayList with an initial capacity of 10.

import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList<String> myList = new ArrayList<String>();
        System.out.println("Initial size of ArrayList: " + myList.size());
    }
}

Output:

Initial size of ArrayList: 0

2. ArrayList(int initialCapacity):

This constructor creates an empty ArrayList with the specified initial capacity. The initial capacity is the number of elements that the ArrayList can store without needing to resize its internal array. If you add more elements than the initial capacity, the ArrayList will automatically resize its internal array to accommodate the additional elements. If you know that you will be storing a large number of elements, it’s a good idea to specify an initial capacity that is greater than the default capacity of 10.

For example:

In this constructor creates an empty ArrayList with the specified initial capacity.

import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList<Integer> myList = new ArrayList<Integer>(20);
        System.out.println("Initial size of ArrayList: " + myList.size());
        System.out.println("Initial capacity of ArrayList: " + myList.capacity());
    }
}

Output:

Initial size of ArrayList: 0
Initial capacity of ArrayList: 20

3. ArrayList(Collection<? extends E> c):

This constructor creates an ArrayList that contains the elements of the specified collection, in the order they are returned by the collection’s iterator. The <? extends E> syntax is a generic wildcard that means “some subtype of E”. In other words, this constructor can accept a collection of any type that is a subtype of the type of elements stored in the ArrayList. For example, if you want to create an ArrayList of strings containing the elements of a LinkedList of strings, you can do the following:

LinkedList<String> myList = new LinkedList<String>();
myList.add("foo");
myList.add("bar");
myList.add("baz");
ArrayList<String> myArrayList = new ArrayList<String>(myList);

In this case, the ArrayList constructor takes a LinkedList of strings (LinkedList<String>) as its argument, but it’s able to create an ArrayList of strings (ArrayList<String>) because LinkedList is a subtype of List, which is a subtype of Collection, and the generic wildcard allows for any subtype of the element type.

For example:

For This constructor example, creates an ArrayList that contains the elements of the specified collection, in the order they are returned by the collection’s iterator.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collection;

public class Example {
    public static void main(String[] args) {
        Collection<Character> myCollection = new HashSet<Character>();
        myCollection.add('a');
        myCollection.add('b');
        myCollection.add('c');
        ArrayList<Character> myList = new ArrayList<Character>(myCollection);
        System.out.println("ArrayList elements: " + myList);
    }
}

Output:

ArrayList elements: [a, b, c]

In this example, we first created a HashSet of characters and added some elements to it. Then we passed this HashSet to the ArrayList constructor to create a new ArrayList containing the same elements. The output shows that the ArrayList contains the elements in the same order as they were added to the HashSet.

List of ArrayList Methods in Java

An ArrayList provides several methods for adding, removing, and accessing elements in the list, as well as methods for manipulating the size and capacity of the list.

Here is a list of some commonly used methods in the ArrayList class in Java:

add(E element): Adds the specified element to the end of the ArrayList.
add(int index, E element):Inserts the specified element at the specified position in the ArrayList.
remove(int index): Removes the element at the specified position in the ArrayList.
remove(Object o): Removes the first occurrence of the specified element from the ArrayList.
get(int index): Returns the element at the specified position in the ArrayList.
set(int index, E element): Replaces the element at the specified position in the ArrayList with the specified element.
size():Returns the number of elements in the ArrayList.
clear(): Removes all of the elements from the ArrayList.
isEmpty():Returns true if the ArrayList contains no elements.
contains(Object o): Returns true if the ArrayList contains the specified element.
indexOf(Object o): Returns the index of the first occurrence of the specified element in the ArrayList, or -1 if the element is not found.
lastIndexOf(Object o): Returns the index of the last occurrence of the specified element in the ArrayList, or -1 if the element is not found.
toArray(): Returns an array containing all of the elements in the ArrayList.
addAll(Collection<? extends E> c): Adds all of the elements in the specified collection to the end of the ArrayList.
addAll(int index, Collection<? extends E> c): Inserts all of the elements in the specified collection into the ArrayList at the specified position.
subList(int fromIndex, int toIndex): Returns a view of the portion of the ArrayList between the specified fromIndex, inclusive, and toIndex, exclusive.

These are just a few examples of the methods available in the ArrayList class. There are many more methods available for working with ArrayLists in Java.

Recommended:

What is arraylist in Java?

JavaScript Array

What is variable in JavaScript?

How to define constant in java?

What is the Console in JavaScript.

SQL introduction

What are the basic SQL Command ?

What is array reduce()method in JavaScript?

Reference:

learnwithshikha20@insta

learnwithshikha@facebook

Leave a Comment