Skip to content

Java Programming · Week 7 notes

Arrays

Introduction to Arrays

What are Arrays?

Arrays are a fundamental concept in Java programming. They allow you to store multiple values of the same type in a single variable. Think of an array as a container that can hold a fixed number of items of the same kind.

Key Concepts

  • An array is a collection of similar types of data
  • Arrays have a fixed size, which is determined when they are created
  • Array elements are stored in contiguous memory locations
  • Each element in an array can be accessed using its index (position)
  • Array indices start at 0, not 1

Declaring an Array

// Method 1 int[] numbers; // Method 2 int numbers[];

Creating an Array

// Create an array of integers with size 5 int[] numbers = new int[5]; // Create and initialize an array int[] numbers = {1, 2, 3, 4, 5};

Accessing Array Elements

int[] numbers = {10, 20, 30, 40, 50}; // Access the first element (index 0) System.out.println(numbers[0]); // Outputs: 10 // Access the third element (index 2) System.out.println(numbers[2]); // Outputs: 30

Why Use Arrays?

  • Efficient storage of multiple items of the same type
  • Easy access to elements using indices
  • Useful for storing and manipulating collections of data
  • Foundation for more complex data structures

Array Basics and Operations

Working with Arrays

Now that we understand what arrays are, let's look at some basic operations we can perform with them.

Initializing Arrays

// Method 1: Declare and then initialize int[] numbers = new int[5]; numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50; // Method 2: Declare and initialize in one line int[] numbers = {10, 20, 30, 40, 50};

Array Length

You can find out how many elements an array has using the length property:

int[] numbers = {10, 20, 30, 40, 50}; System.out.println("Array length: " + numbers.length); // Outputs: Array length: 5

Iterating Through an Array

You can use a loop to go through all elements of an array:

int[] numbers = {10, 20, 30, 40, 50}; // Using a for loop for (int i = 0; i < numbers.length; i++) { System.out.println("Element at index " + i + ": " + numbers[i]); } // Using a for-each loop (enhanced for loop) for (int number : numbers) { System.out.println("Element: " + number); }

Modifying Array Elements

You can change the value of an array element by assigning a new value to a specific index:

int[] numbers = {10, 20, 30, 40, 50}; numbers[2] = 35; // Change the third element (index 2) to 35 System.out.println("Updated third element: " + numbers[2]); // Outputs: Updated third element: 35

Common Array Algorithms

Common Array Algorithms

Arrays are often used in various algorithms. Let's look at some common operations you might perform on arrays.

1. Finding the Sum of Array Elements

int[] numbers = {5, 10, 15, 20, 25}; int sum = 0; for (int number : numbers) { sum += number; } System.out.println("Sum of array elements: " + sum); // Outputs: Sum of array elements: 75

2. Finding the Largest Element

int[] numbers = {12, 45, 7, 23, 56, 89, 34}; int largest = numbers[0]; // Assume first element is largest for (int i = 1; i < numbers.length; i++) { if (numbers[i] > largest) { largest = numbers[i]; } } System.out.println("Largest element: " + largest); // Outputs: Largest element: 89

3. Reversing an Array

int[] numbers = {1, 2, 3, 4, 5}; int[] reversed = new int[numbers.length]; for (int i = 0; i < numbers.length; i++) { reversed[i] = numbers[numbers.length - 1 - i]; } System.out.println("Reversed array: " + Arrays.toString(reversed)); // Outputs: Reversed array: [5, 4, 3, 2, 1]