Chapter 7 - Arrays & the ArrayList

7.1 Introduction to Arrays

  • Definition: An array is a collection of elements of the same data type, stored in contiguous memory locations.
  • Purpose: Used to store multiple values efficiently.

Syntax

int[] arrayName = new int[size];
Example
int[] numbers = new int[5];
  • Declaration and Initialization
int[] numbers = {1, 2, 3, 4, 5};

7.2 Accessing Array Elements

  • Indexing: Use zero-based index to access elements.
System.out.println(numbers[0]); // Output: 1
  • Modify Elements
numbers[2] = 10; // Updates the third element to 10
  • Array Length
System.out.println(numbers.length); // Output: 5

7.3 Types of Arrays

1D Arrays
  • Stores data in a linear format.
  • Example
  • double[] prices = new double[3];
  • prices[0] = 10.5;
  • prices[1] = 20.0;
prices[2] = 30.5;
2D Arrays
  • Stores data in rows and columns (matrix).
  • Syntax
int[][] matrix = new int[rows][cols];
  • Example
  • int[][] matrix = {
  • {1, 2, 3},
  • {4, 5, 6},
  • {7, 8, 9}
  • };
System.out.println(matrix[1][2]); // Output: 6

Jagged Arrays

  • Arrays with rows of varying lengths.
  • Example
  • int[][] jaggedArray = new int[3][];
  • jaggedArray[0] = new int[2];
  • jaggedArray[1] = new int[3];
jaggedArray[2] = new int[1];

7.4 Looping Through Arrays

Using For Loop

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

Using Enhanced For Loop

for (int num : numbers) {
    System.out.println(num);
}

7.5 Common Array Operations

  1. Finding Maximum/Minimum
  1. int max = numbers[0];
  2. for (int num : numbers) {
  3. if (num > max) {
  4. max = num;
  5. }
}
  1. Summing Elements
  1. int sum = 0;
  2. for (int num : numbers) {
  3. sum += num;
}
  1. Reversing an Array
  • for (int i = 0, j = numbers.length – 1; i < j; i++, j–) {
  1. int temp = numbers[i];
  2. numbers[i] = numbers[j];
  3. numbers[j] = temp;
}

7.6 Multidimensional Arrays

  • Initialization
int[][] matrix = new int[3][3];
  • Nested Loops for Traversal
  • for (int i = 0; i < matrix.length; i++) {
  • for (int j = 0; j < matrix[i].length; j++) {
  • out.println(matrix[i][j]);
  • }
}

7.7 Array Methods in Java

  1. Sorting an Array
Arrays.sort(numbers);
  1. Copying Arrays
int[] copy = Arrays.copyOf(numbers, numbers.length);
  1. Comparing Arrays
boolean isEqual = Arrays.equals(arr1, arr2);
  1. Filling Arrays
Arrays.fill(numbers, 5); // Fills array with 5

7.8 Limitations of Arrays

  1. Fixed Size: Cannot dynamically resize after creation.
  2. Homogeneous: Can store only one type of data.
  3. No Built-in Methods for Complex Operations: Use libraries like ArrayList for dynamic arrays.

7.9 Best Practices for Using Arrays

  1. Always validate array indices to avoid ArrayIndexOutOfBoundsException.
  2. Use enhanced for-loops for readability when modification isn’t required.
  3. Prefer using Java’s Arrays class for common operations.

Summary

  • Arrays provide an efficient way to store and access multiple values of the same type.
  • Mastering array operations is essential for solving problems involving data manipulation.
  • Use multidimensional arrays for matrix-like structures and jagged arrays for irregular data layouts.

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.