Difference between Array and Collection

Difference between Array and Collection

Array and Collection

In Java, both array and collection are used to store and manipulate groups of elements. However, they differ in terms of their characteristics, usage, and flexibility.

Array

Explanation: An array in Java is a fixed-size data structure that stores elements of the same data type in contiguous memory locations. Arrays have a fixed length determined at the time of creation and can store primitives or objects.

Collection

Explanation: Collection in Java is a framework that provides classes and interfaces to store and manipulate groups of objects. Unlike arrays, collections can dynamically grow and shrink in size. They are more flexible and provide various data structures like lists, sets, queues, etc.

Difference between Array and Collection

Differences Between Array and Collection

1. Size: Explanation: Arrays have a fixed size determined at initialization and cannot change dynamically without creating a new array. Collections can grow or shrink in size as elements are added or removed.

2. Type Flexibility: Explanation: Arrays can store elements of a specific data type (primitive or object), whereas collections can store elements of any type using generics.

3. Mutability: Explanation: Arrays are mutable in terms of updating elements at specific indices but not in terms of resizing. Collections provide mutability with operations like adding, removing, and modifying elements.

4. Performance: Explanation: Arrays generally have better performance for direct element access (random access) due to their contiguous memory layout. Collections may have slightly higher overhead due to their dynamic resizing and additional functionalities.

5.Usage: Explanation: Arrays are commonly used for simple data structures and when the size is known in advance. Collections are preferred when flexibility, dynamic resizing, or specific functionalities (e.g., sorting, searching) are required.

Example in Java

Let’s illustrate the differences between arrays and collections with Java examples.

Array Example
java
// Array of integers
int[] intArray = new int[5];
intArray[0] = 10;
intArray[1] = 20;
intArray[2] = 30;

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


- Explanation: In this example, intArray is an array of integers with a fixed size of 5. Elements are accessed using index notation (intArray[1]) for direct access.

Collection Example (ArrayList)
java
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        // ArrayList of Strings
        List<String> stringList = new ArrayList<>();
        stringList.add("Apple");
        stringList.add("Banana");
        stringList.add("Orange");

        // Accessing elements
        System.out.println("Element at index 1: " + stringList.get(1)); // Output: Element at index 1: Banana
    }
}

Homepage

Readmore