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
-
Finding Maximum/Minimum
-
int max = numbers[0];
-
for (int num : numbers) {
-
if (num > max) {
-
max = num;
-
}
}
-
Summing Elements
-
int sum = 0;
-
for (int num : numbers) {
-
sum += num;
}
-
Reversing an Array
-
for (int i = 0, j = numbers.length – 1; i < j; i++, j–) {
-
int temp = numbers[i];
-
numbers[i] = numbers[j];
-
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
-
Sorting an Array
Arrays.sort(numbers);
-
Copying Arrays
int[] copy = Arrays.copyOf(numbers, numbers.length);
-
Comparing Arrays
boolean isEqual = Arrays.equals(arr1, arr2);
-
Filling Arrays
Arrays.fill(numbers, 5); // Fills array with 5
7.8 Limitations of Arrays
-
Fixed Size: Cannot dynamically resize after creation.
-
Homogeneous: Can store only one type of data.
-
No Built-in Methods for Complex Operations: Use libraries like ArrayList for dynamic arrays.
7.9 Best Practices for Using Arrays
-
Always validate array indices to avoid ArrayIndexOutOfBoundsException.
-
Use enhanced for-loops for readability when modification isn’t required.
-
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.