Chapter 6: Arrays
Introduction to Arrays
Array is a collection of similar types of data.
Use Case: Storing marks of 5 students instead of creating 5 separate variables.
Array Memory Representation
Reference Variable
marks
Array Object
5 × 4 = 20 bytes
Array Declaration and Initialization
Three Ways to Create Arrays:
// Method 1: Declaration + Memory Allocation
int[] marks; // Declaration
marks = new int[5]; // Memory Allocation
// Method 2: Declaration + Memory Allocation (Combined)
int[] marks = new int[5];
// Method 3: Declaration + Initialize
int[] marks = {100, 70, 80, 71, 98};
Accessing Array Elements
public class ArrayAccess {
public static void main(String[] args) {
int[] marks = new int[5];
// Setting values (Note: index starts from 0)
marks[0] = 100;
marks[1] = 70;
marks[2] = 80;
marks[3] = 71;
marks[4] = 98;
// Accessing values
System.out.println("First student marks: " + marks[0]);
System.out.println("Last student marks: " + marks[4]);
// Array length
System.out.println("Total students: " + marks.length);
}
}
Important: Array indices start from 0 and go till (n-1) where n is the size of the array.
Array Operations
Array Length Property
Arrays have a length property which gives the length of the array.
marks.length gives 5 if marks is an array with 5 elements.
Displaying an Array
public class ArrayTraversal {
public static void main(String[] args) {
int[] marks = {85, 92, 78, 96, 88};
// Method 1: Traditional for loop
System.out.println("Using traditional for loop:");
for (int i = 0; i < marks.length; i++) {
System.out.println("Student " + (i+1) + ": " + marks[i]);
}
// Method 2: Enhanced for loop (for-each)
System.out.println("\nUsing enhanced for loop:");
for (int mark : marks) {
System.out.println("Marks: " + mark);
}
}
}
Array in Reverse Order
Quick Quiz: Write a Java program to print the elements of an array in reverse order
Multidimensional Arrays
Multidimensional Arrays are Array of Arrays.
Each element of a M-D array is an array itself.
2-D Array
public class TwoDArray {
public static void main(String[] args) {
// Creating a 2-D array of 2 rows and 3 columns
int[][] flats = new int[2][3];
// Adding elements to this array
flats[0][0] = 101;
flats[0][1] = 102;
flats[0][2] = 103;
flats[1][0] = 201;
flats[1][1] = 202;
flats[1][2] = 203;
// Displaying the 2-D array
System.out.println("2-D Array:");
for (int i = 0; i < flats.length; i++) {
for (int j = 0; j < flats[i].length; j++) {
System.out.print(flats[i][j] + " ");
}
System.out.println();
}
}
}
2-D Array Visualization
| Col 0 | Col 1 | Col 2 | |
|---|---|---|---|
| Row 0 | (0,0) | (0,1) | (0,2) |
| Row 1 | (1,0) | (1,1) | (1,2) |
3-D Array
public class ThreeDArray {
public static void main(String[] args) {
// Creating a 3-D array
String[][][] arr = new String[2][3][4];
// Example: Building floors, rooms, items
arr[0][0][0] = "Chair";
arr[0][0][1] = "Table";
arr[1][2][3] = "Lamp";
System.out.println("3-D Array element: " + arr[0][0][0]);
}
}
Practice Set
Chapter 6 - Practice Set
1. Array Sum
Create an array of 5 floats and calculate their sum.
2. Element Search
Write a program to find out whether a given integer is present in an array or not.
3. Average Calculation
Calculate the average marks from an array containing marks of all students in Physics using for-each loop.
4. Matrix Addition
Create a Java program to add two matrices of size 2×3.
5. Array Reversal
Write a Java program to reverse an array.
6. Maximum Element
Write a Java program to find the maximum element in an array.
7. Minimum Element
Write a Java program to find the minimum element in a Java array.
8. Sorted Array Check
Write a Java program to find whether an array is sorted or not.