Arrays, ArrayLists, and Searching in Java

April 20, 2025

javaarraysdata structuresalgorithmssearching

Arrays, ArrayLists, and Searching in Java

This tutorial covers fundamental data structures and algorithms in Java: arrays, ArrayLists, and searching techniques. Understanding these concepts is essential for efficient data manipulation and problem-solving in programming.

Arrays in Java

An array is a fixed-size collection of elements of the same type. Arrays provide a convenient way to store and access multiple values using a single variable name.

Declaring and Initializing Arrays

// Declaration
int[] numbers;            // Preferred syntax
int scores[];             // Alternative syntax

// Initialization
numbers = new int[5];     // Creates an array of 5 integers with default values (0)

// Declaration and initialization in one line
int[] values = new int[3];

// Declaration and initialization with specific values
int[] primes = {2, 3, 5, 7, 11};

// Multi-dimensional arrays
int[][] matrix = new int[3][4];  // 3 rows, 4 columns

Accessing and Modifying Array Elements

int[] numbers = {10, 20, 30, 40, 50};

// Accessing elements (zero-based indexing)
int firstElement = numbers[0];   // 10
int thirdElement = numbers[2];   // 30

// Modifying elements
numbers[1] = 25;                 // Changes 20 to 25

// Array length
int length = numbers.length;     // 5

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

// Enhanced for loop (for-each)
for (int num : numbers) {
    System.out.println(num);
}

Array Visualization

10
25
30
40
50
0
1
2
3
4

Array indices (zero-based)

Limitations of Arrays

  1. Fixed size: Once created, the size of an array cannot be changed.
  2. Homogeneous elements: All elements must be of the same type.
  3. Manual resizing: To add more elements than the initial capacity, you need to create a new, larger array and copy the elements.

ArrayList in Java

ArrayList is a part of Java’s Collection Framework and provides a dynamic array implementation. Unlike arrays, ArrayLists can grow or shrink in size.

Creating and Using ArrayLists

import java.util.ArrayList;

// Creating an ArrayList
ArrayList<Integer> numbers = new ArrayList<>();  // Empty ArrayList
ArrayList<String> names = new ArrayList<>(20);   // Initial capacity of 20

// Adding elements
numbers.add(10);                  // Adds 10 to the end
numbers.add(20);                  // Adds 20 to the end
numbers.add(0, 5);                // Adds 5 at index 0, shifting other elements

// Accessing elements
int first = numbers.get(0);       // 5
int second = numbers.get(1);      // 10

// Modifying elements
numbers.set(1, 15);               // Changes 10 to 15

// Removing elements
numbers.remove(0);                // Removes element at index 0
numbers.remove(Integer.valueOf(20)); // Removes the first occurrence of 20

// Size of ArrayList
int size = numbers.size();        // Number of elements

// Checking if an element exists
boolean contains = numbers.contains(15);  // true

// Iterating through an ArrayList
for (int i = 0; i < numbers.size(); i++) {
    System.out.println(numbers.get(i));
}

// Enhanced for loop
for (Integer num : numbers) {
    System.out.println(num);
}

ArrayList vs Array

Array vs ArrayList Comparison

FeatureArrayArrayList
SizeFixedDynamic
TypePrimitive & ObjectsObjects only
PerformanceFasterSlower
FunctionalityBasicRich methods

Searching Algorithms

Searching is the process of finding a specific element in a collection of elements. Let’s explore two common searching algorithms.

Linear search is the simplest searching algorithm that checks each element in the collection sequentially until the target element is found or the collection is exhausted.

public static int linearSearch(int[] arr, int target) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == target) {
            return i;  // Return the index of the found element
        }
    }
    return -1;  // Element not found
}

Linear search works on both sorted and unsorted arrays but is inefficient for large datasets.

Binary search is an efficient algorithm for finding an element in a sorted array. It works by repeatedly dividing the search interval in half.

public static int binarySearch(int[] arr, int target) {
    int left = 0;
    int right = arr.length - 1;

    while (left <= right) {
        int mid = left + (right - left) / 2;

        // Check if target is present at mid
        if (arr[mid] == target) {
            return mid;
        }

        // If target is greater, ignore left half
        if (arr[mid] < target) {
            left = mid + 1;
        }
        // If target is smaller, ignore right half
        else {
            right = mid - 1;
        }
    }

    return -1;  // Element not found
}

Binary Search Visualization

10
20
30
40
50
60
70
left
mid
right

Searching for 40: Found at mid position!

  • Linear Search:

    • Works on both sorted and unsorted arrays
    • Simple to implement
    • Inefficient for large datasets
    • Checks each element one by one
  • Binary Search:

    • Requires a sorted array
    • More complex to implement
    • Very efficient for large datasets
    • Eliminates half of the remaining elements in each step

Using Java’s Built-in Search Methods

Java provides built-in methods for searching:

// For arrays
import java.util.Arrays;

int[] numbers = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(numbers, 30); // Returns 2

// For ArrayLists
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50));
int position = list.indexOf(30); // Returns 2
boolean contains = list.contains(60); // Returns false

Conclusion

Arrays and ArrayLists are fundamental data structures in Java, each with its own advantages and use cases. Arrays offer simplicity and performance, while ArrayLists provide flexibility and convenience.

When it comes to searching, the choice between linear and binary search depends on:

  1. Whether the data is sorted
  2. The size of the dataset
  3. How frequently searches are performed

For most applications, using Java’s built-in search methods is recommended as they are optimized and well-tested. Understanding these concepts will help you write more efficient and effective Java programs.