How to make arraylist in Java?

Creating an ArrayList in Java: A Comprehensive Guide

What is an ArrayList?

An ArrayList is a resizable array of objects that can be accessed by their index. It is a fundamental data structure in Java that allows for efficient insertion, deletion, and searching of elements. In this article, we will explore how to create an ArrayList in Java, including its advantages, key features, and examples of its usage.

Why Use an ArrayList?

Before creating an ArrayList, it’s essential to understand its advantages:

  • Fast access: Arrays require indexing, which can be slow for large datasets.
  • Memory efficiency: Arrays use contiguous memory, which can lead to memory waste.
  • Resizability: Arrays can grow or shrink dynamically, making them suitable for applications with variable sizes.

Creating an ArrayList

To create an ArrayList, you can use the following methods:

  • New ArrayList(): This method creates an empty ArrayList.
  • ArrayList(): This method creates an ArrayList with a specified initial capacity.
  • extend(): This method adds elements to the end of the ArrayList.
  • add(): This method adds an element to the beginning of the ArrayList.
  • get(): This method returns an element at a specified index.
  • size(): This method returns the number of elements in the ArrayList.

Example: Creating an ArrayList with New ArrayList()

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
System.out.println(list);
}
}

Output:

[]

Example: Creating an ArrayList with ArrayList()

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(10);
System.out.println(list);
}
}

Output:

[]

Example: Creating an ArrayList with extend()

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list);
}
}

Output:

[Apple, Banana, Cherry]

Example: Creating an ArrayList with add()

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Orange");
list.add("Grapes");
list.add("Peach");
System.out.println(list);
}
}

Output:

[Orange, Grapes, Peach]

Example: Creating an ArrayList with get()

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
System.out.println(list.get(0));
System.out.println(list.get(1));
}
}

Output:

Orange
Grapes

Example: Creating an ArrayList with size()

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
System.out.println(list.size());
}
}

Output:

0

Advantages of ArrayList

  • Fast access: ArrayList provides constant-time performance for access, making it suitable for applications with high performance requirements.
  • Resizability: ArrayList can grow or shrink dynamically, making it suitable for applications with variable sizes.
  • Memory efficiency: ArrayList uses contiguous memory, which can lead to memory waste, but can also improve cache performance.

Common ArrayList Methods

  • add(E o): Adds an element to the end of the ArrayList.
  • add(int index, E o): Adds an element to the beginning of the ArrayList at the specified index.
  • contains(E o): Checks if the ArrayList contains the specified element.
  • contains(Object o): Checks if the ArrayList contains the specified object.
  • indexOf(E o): Returns the index of the first occurrence of the specified element.
  • indexOf(Object o): Returns the index of the first occurrence of the specified object.
  • iterator(): Returns an iterator that traverses the ArrayList.
  • size(): Returns the number of elements in the ArrayList.

Common ArrayList Operators

  • size(): Returns the number of elements in the ArrayList.
  • get(index): Returns the element at the specified index.
  • set(index, o): Sets the element at the specified index.
  • isEmpty(): Checks if the ArrayList is empty.
  • containsAll(Collection c): Checks if the ArrayList contains all elements of the specified collection.
  • containsAll(Collection c, Object o): Checks if the ArrayList contains all elements of the specified collection and the specified object.

Common ArrayList Usage

  • Implementing a Stack: ArrayList can be used as a stack, where elements are added and removed from the top of the stack.
  • Implementing a Queue: ArrayList can be used as a queue, where elements are added to the end of the queue and removed from the front of the queue.
  • Implementing a Map: ArrayList can be used as a key-value map, where elements are stored in the keys and values.

Conclusion

In this article, we explored the creation of an ArrayList in Java, including its advantages, key features, and examples of its usage. We also covered the various methods and operators available for working with ArrayLists, as well as common applications and use cases. With its fast access, resizability, and memory efficiency, ArrayList is a fundamental data structure in Java that can be used in a wide range of applications.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

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

Scroll to Top