Arrays & Collections Of Java

 In Java, arrays and collections are essential for storing and manipulating groups of elements. Arrays are fixed-size data structures that store elements of the same type contiguously in memory, while collections are dynamic data structures that can store elements of different types and sizes. Here's an overview of arrays and collections in Java:

Arrays:

  1. Declaration and Initialization:

    • Arrays are declared using square brackets [].
    • They can be initialized using either an array literal or the new keyword.

    • // Declaration and initialization of an array int[] numbers = {1, 2, 3, 4, 5};

  2. Accessing Elements:

    • Elements in an array are accessed using zero-based indices.

    int thirdNumber = numbers[2]; // Accessing the third element (index 2)
  1. Length of Array:

    • The length of an array can be obtained using the length property.

    int arrayLength = numbers.length; // Length of the array

Iterating Over Array:

  • Arrays can be traversed using loops like for or foreach.

for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

Collections:

  1. ArrayList:

    • ArrayList is a dynamic array implementation provided by the Java Collections Framework.
    • It allows resizing and manipulation of elements.

  2. // Creating an ArrayList of Strings ArrayList<String> names = new ArrayList<>(); // Adding elements to the ArrayList names.add("Alice"); names.add("Bob"); names.add("Charlie"); // Accessing elements by index String secondName = names.get(1); // Bob

HashMap:

  • HashMap is a key-value pair collection that allows storing elements based on unique keys.

// Creating a HashMap with Integer keys and String values HashMap<Integer, String> studentMap = new HashMap<>(); // Adding elements to the HashMap studentMap.put(1, "Alice"); studentMap.put(2, "Bob"); studentMap.put(3, "Charlie"); // Accessing elements by key String studentName = studentMap.get(2); // Bob

HashSet:

  • HashSet is a collection that stores unique elements, eliminating duplicates.

// Creating a HashSet of integers HashSet<Integer> numbersSet = new HashSet<>(); // Adding elements to the HashSet numbersSet.add(1); numbersSet.add(2); numbersSet.add(3); numbersSet.add(2); // Duplicate element, will be ignored // Iterating over the HashSet for (Integer num : numbersSet) { System.out.println(num); }

LinkedList:

  • LinkedList is a doubly-linked list implementation that provides efficient insertion and deletion operations.

// Creating a LinkedList of Strings LinkedList<String> linkedList = new LinkedList<>(); // Adding elements to the LinkedList linkedList.add("Apple"); linkedList.add("Banana"); linkedList.add("Orange"); // Removing the first element linkedList.removeFirst(); // Iterating over the LinkedList for (String fruit : linkedList) { System.out.println(fruit); }

Arrays and collections are fundamental data structures in Java that facilitate efficient manipulation and management of elements. Depending on the requirements, developers choose between arrays and various collection types to store and process data effectively.

Post a Comment

0 Comments