Java ArrayList

As we discussed about Arrays in java previous lecture, here today we are going to see the Java Arraylist.

What is Java Arraylist?

ArrayList is nothing but a class that uses a dynamic array to store the value. It has no size limit we can add remove the element anytime simply we can say that it is a resizable array. It is present in java.util package. It might be slower than a normal array but it can be helpful in programs where lots of manipulation in the array is needed. ArrayLists are similar to vectors in C++.

How to create Java ArrayList?

 It is required to specify the data type of elements in an ArrayList and it is up to the user to define its initial size.

import java.util.ArrayList; // importing the ArrayList class 
ArrayList<String> arrayList = new ArrayList<String>(); //string arraylist
Example:
ArrayList<String> cars = new ArrayList<String>();//Creating an ArrayList object called cars that is storing strings

Java ArrayList also has a number of constructors. Let us look at each of them below.

ArrayList Constructor

The Bellow following points describes the constructors that a Java ArrayList contains.

  1. ArrayList()
  2. This constructor builds an empty ArrayList.
  3. ArrayList(Collection<? extends E> c)
  4. It builds an ArrayList initialized with Collection c’s elements.
  5. ArrayList(int capacity)
  6. It creates an ArrayList that has the specified initial capacity.

Methods of ArrayList

There are bunch of methods available which can be used directly using object of ArrayList in java.

  1. add( ): This method adds an object o to the arraylist.
import java.util.ArrayList;

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

Output:

java ArrayList

2. .get(): For accessing an element from a arraylist, we use this method.

public class Main { 
  public static void main(String[] args) { 
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    System.out.println(cars.get(0));
  } 
}
Output: BMW

3. .set(): For modifying any element in a arraylist, we use this method.

import java.util.ArrayList;

public class Main { 
  public static void main(String[] args) { 
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    cars.set(0, "Audi");
    System.out.println(cars);
  } 
}
Output:
[Audi, Ford, Mazda]

4. .remove(): For removing a item, we use .remove() method.

import java.util.ArrayList;

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

Java ArrayList Size:

To find out total size of an ArrayList have, we use the size method:

import java.util.ArrayList;

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

Output: 3


Faqs on Java ArrayList:

What is .sort() method in Java?

In the java.util package, there is a useful class i.e. collection class, which include .sort() method for sorting lists alphabetically or numerically. Syntax: Collections.sort(objectname);
Let us suppose that our list contains {“technical”, “blog”, “arraylist”, “example”}
after applying sort method we get {“arraylist”,”blog”,”example”,”technical”}

How to synchronize Array List in Java?

By Default, ArrayList is not synchronized, but it is possible to create a synchronized ArrayList by mentioned two approaches using the Collections.synchronizedList() method
Using thread-safe variant of ArrayList that is : CopyOnWriteArrayList

What is CopyOnWriteArrayList ?

CopyOnWriteArrayList is a Java concurrent Collection class, and it implements a List interface like ArrayList, Vector, and LinkedList.
But it is a thread-safe collection, and it achieves its thread-safety in a somewhat different way than a Vector.
As the name we can say that a CopyOnWriteArrayList creates a copy of underlying ArrayList with every mutation operation e.g. add or set operation.
Normally. CopyOnWriteArrayList is expensive because it involves costly Array copy with every writes operation. But it’s very handy if we have a List where Iteration outnumbers mutation e.g. we mostly need to iterate the ArrayList and don’t modify it too often.
 

How to get sublist from ArrayList in Java?

You can get a list of elements by using .subList() method. This method would be quite helpful in the case of a sorted list.

2 thoughts on “Java ArrayList

Leave a Reply

Your email address will not be published. Required fields are marked *