Arrays & Methods Lesson 14 of 21
Java Arrays and 2D Arrays
Store many values of the same type in an array — create, read, change and loop through arrays, use the for-each loop, Arrays.sort and Arrays.toString, and work with 2D arrays like a seating chart.
An array holds a fixed number of values of the same type, like the grades of 40 students. Instead of 40 variables, you have one array with 40 numbered slots.
Creating arrays
int[] grades = {88, 92, 79, 95, 84}; // with values
String[] subjects = {"Math", "Science", "English"};
double[] prices = new double[10]; // 10 slots, all 0.0
When you use new type[size], every slot starts with a default value: 0 for numbers, false for booleans and null for Strings.
Indexes
public class Main {
public static void main(String[] args) {
int[] grades = {88, 92, 79, 95, 84};
System.out.println("First: " + grades[0]);
System.out.println("Third: " + grades[2]);
System.out.println("Last: " + grades[grades.length - 1]);
System.out.println("Count: " + grades.length);
grades[2] = 81; // change a value
System.out.println("Third is now: " + grades[2]);
}
}
Output
First: 88 Third: 79 Last: 84 Count: 5 Third is now: 81
Indexes start at 0, so an array of 5 items has indexes 0 to 4. length (no parentheses for arrays) gives the size.
Looping through an array
public class Main {
public static void main(String[] args) {
int[] grades = {88, 92, 79, 95, 84};
int sum = 0;
int highest = grades[0];
for (int i = 0; i < grades.length; i++) {
sum += grades[i];
if (grades[i] > highest) highest = grades[i];
}
double average = (double) sum / grades.length;
System.out.printf("Sum: %d, Average: %.2f, Highest: %d%n", sum, average, highest);
}
}
Output
Sum: 438, Average: 87.60, Highest: 95
The for-each loop
When you need each value but not its index, the enhanced for is simpler:
public class Main {
public static void main(String[] args) {
String[] officers = {"Ana", "Ben", "Carla"};
for (String name : officers) {
System.out.println("Officer: " + name);
}
}
}
Output
Officer: Ana Officer: Ben Officer: Carla
Read it as "for each name in officers". You can't change the array through the loop variable, though; use a normal for for that.
The Arrays helper class
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] scores = {72, 95, 88, 60, 81};
System.out.println(Arrays.toString(scores));
Arrays.sort(scores);
System.out.println(Arrays.toString(scores));
System.out.println(Arrays.stream(scores).sum());
int[] copy = Arrays.copyOf(scores, scores.length);
copy[0] = 100;
System.out.println(scores[0] + " vs " + copy[0]);
}
}
Output
[72, 95, 88, 60, 81] [60, 72, 81, 88, 95] 396 60 vs 100
Printing an array directly, like System.out.println(scores), shows something useless like [I@1b6d3586. Use Arrays.toString().
Parallel arrays
public class Main {
public static void main(String[] args) {
String[] items = {"Pandesal", "Kape", "Itlog"};
double[] prices = {25, 8, 9};
int[] qty = {1, 4, 6};
double total = 0;
for (int i = 0; i < items.length; i++) {
double line = prices[i] * qty[i];
System.out.printf("%-10s x%d ₱%6.2f%n", items[i], qty[i], line);
total += line;
}
System.out.printf("TOTAL ₱%6.2f%n", total);
}
}
Output
Pandesal x1 ₱ 25.00 Kape x4 ₱ 32.00 Itlog x6 ₱ 54.00 TOTAL ₱111.00
Items at the same index belong together. Later, you'll replace parallel arrays with an array of objects, which is cleaner.
2D arrays
A 2D array is an array of arrays, like a table with rows and columns:
public class Main {
public static void main(String[] args) {
String[][] seats = {
{"Ana", "Ben", "Carla"},
{"Dan", "Ela", "Fe"},
{"Gab", "Hans", "Isa"}
};
System.out.println("Row 2, seat 3: " + seats[1][2]);
for (int r = 0; r < seats.length; r++) {
for (int c = 0; c < seats[r].length; c++) {
System.out.printf("%-6s", seats[r][c]);
}
System.out.println();
}
}
}
Output
Row 2, seat 3: Fe Ana Ben Carla Dan Ela Fe Gab Hans Isa
seats[row][column]. seats.length is the number of rows; seats[r].length is the number of columns in row r.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04