Understanding Java Arrays and Their Uses

Aug 30, 2024

Arrays in Java

Introduction

  • Presented by Lakshay from Simply Learn.
  • Part of a comprehensive Java tutorial series.
  • Topics covered: arrays in Java, types of arrays, sorting, and searching in arrays.

What is an Array?

  • Data type containing homogeneous elements (same data type).
  • A group of like-typed variables referred to by a common name.
  • Syntax for declaring an array in Java:
    dataType[] arrayName = new dataType[size];  
    
  • Example:
    • Array of integers: int[] arr = new int[5];
  • Indexing:
    • Starts at 0. Each element has an associated index.

Features of Arrays

  • Dynamically allocated, no need to know memory beforehand.
  • All elements stored under one name (array name).
  • Contiguous memory allocation (unlike linked lists).

Example: Declaring and Using Arrays

  1. Create a new project in IntelliJ, named "Array Program".
  2. Code Example:
    int[] arr = new int[5];  
    arr[0] = 10;  
    arr[1] = 20;  
    arr[2] = 30;  
    arr[3] = 40;  
    arr[4] = 50;  
    
  3. Printing Elements:
    • Use a for loop:
    for(int i = 0; i < arr.length; i++) {  
        System.out.println("Element at index " + i + " is " + arr[i]);  
    }  
    
  4. Error Handling:
    • Adding an extra element causes an "ArrayIndexOutOfBoundsException".

Types of Arrays

  1. One-Dimensional Array (Linear Array)
    • Stores data in a single row.
    • Example: int[] arr = new int[5];
  2. Two-Dimensional Array
    • Stores data in rows and columns.
    • Example:
    int[][] arr = { {1, 2, 3}, {4, 5, 6} };  
    
  3. Multi-Dimensional Array
    • Combination of two or more arrays.

Example: One and Two-Dimensional Arrays

  1. One-Dimensional Array:
    • Code to calculate sum of elements.
    • Example: sum += arr[i];
  2. Two-Dimensional Array:
    • Use nested for loops to access elements.
    for(int i = 0; i < 2; i++) {  
        for(int j = 0; j < 3; j++) {  
            System.out.print(arr[i][j] + " ");  
        }  
        System.out.println();  
    }  
    

Sorting and Searching Arrays

Sorting

  • Use sorting algorithms (e.g., Bubble Sort, Quick Sort).
  • Example: Sorting an array of random elements in ascending order.

Searching

  • Use searching algorithms (e.g., Linear Search, Binary Search).
  • Example: Finding an element in an array, e.g., "11 found at index 5".

Example: Searching and Sorting using Java Code

Binary Search

  1. Binary Search Algorithm:
    • Compares target value to middle element.
    • Halves search space until target is found.
  2. Code Example:
    int binarySearch(int[] arr, int left, int right, int x) {  
        // Implementation  
    }  
    

Bubble Sort

  1. Bubble Sort Algorithm:
    • Compares adjacent pairs, swaps if out of order.
  2. Code Example:
    void bubbleSort(int[] arr) {  
        // Implementation  
    }  
    

Conclusion

  • Covered arrays, types, sorting, and searching techniques in Java.
  • Encouraged practice with examples.