Difference between Array and ArrayList in java

Difference between Array and ArrayList in java

Array and ArrayList are both used to store collections of elements in Java, but they have several differences in terms of their features and usage.

 Array and ArrayList

1. Fixed Size vs. Dynamic Size

  • Array: Array in Java have a fixed size, meaning once an array is created, its size cannot be changed. You need to specify the size of the array when it is created.
  • ArrayList: ArrayLists, on the other hand, have a dynamic size. They can grow or shrink dynamically as elements are added or removed. You don’t need to specify the size of an ArrayList when it is created.

2. Primitives vs. Objects

  • Array: Arrays can store both primitive data types (such as int, char, etc.) and objects. However, arrays are themselves objects in Java.
  • ArrayList: ArrayLists can only store objects, not primitive data types directly. If you want to store primitive data types, you need to use their corresponding wrapper classes (e.g., Integer for int, Character for char) or use Java generics Array and ArrayList.

3. Direct Access vs. Method Invocation

  •  Array: Elements in an array can be accessed directly using index notation (array[index]).
  •  ArrayList: Elements in an ArrayList are accessed using methods such as get(index) and set(index, element).

4. Type Safety Array and ArrayList

  • Array: Arrays provide no built-in type safety, which means you can store elements of different types in the same array.
  • ArrayList: ArrayLists provide type safety through generics. You specify the type of elements the ArrayList will contain when you create it (e.g., ArrayList<String>), and the compiler ensures that only elements of that type can be added to the Array and ArrayList.

Now, let’s provide Java code examples to illustrate these differences:

Example
java
// Array Example
public class ArrayExample {
    public static void main(String[] args) {
        // Creating an array of integers with a fixed size
        int[] array = new int[3];

        // Assigning values to array elements
        array[0] = 10;
        array[1] = 20;
        array[2] = 30;

        // Accessing array elements directly
        System.out.println("Element at index 1: " + array[1]); // Output: 20
    }
}

Example
java
import java.util.ArrayList;

// ArrayList Example
public class ArrayListExample {
    public static void main(String[] args) {
        // Creating an ArrayList of integers
        ArrayList<Integer> arrayList = new ArrayList<>();

        // Adding elements to the ArrayList
        arrayList.add(10);
        arrayList.add(20);
        arrayList.add(30);

        // Accessing elements using methods
        System.out.println("Element at index 1: " + arrayList.get(1)); // Output: 20
    }
}

Array Example

In the array example, we create an array of integers with a fixed size of 3. We assign values to array elements using index notation (array[index]), and we can directly access elements using the same notation.

ArrayList example

We create an ArrayList of integers. We add elements to the ArrayList using the add() method, and we access elements using the get() method. The methods of the ArrayList class provide a way to interact with the elements since ArrayLists are objects and don’t support direct access like arrays. Additionally, notice the use of generics (ArrayList) to ensure type safety.

Homepage

Readmore