Object-Oriented Programming through Java · Arrays and Inheritance
Arrays
In Object-Oriented Programming through Java because a hall is not one seat — it is a numbered row, then a grid, and Java's Arrays class sorts and searches that row.
Meera's hall is a list of seats of the same type. An array gives each seat an index starting at 0. A second dimension is a row of rows. A third is rare and still just arrays inside arrays. java.util.Arrays sorts and searches the one-dimensional case.
- Object-Oriented Programming through Java
- Easy level
- 6 concepts
1An array is a numbered row of one type
An array is a fixed-length row of slots that all hold the same type. Meera's three seats are String[] seats, not seat1, seat2, seat3 as unrelated names.
The length is part of the object and does not grow. If the hall adds a seat, you need a new array.
Figure. Three slots, one type, indices 0, 1 and 2. Not three loose variables.
How it works
- An array is a numbered row of one typeOne type, a fixed length, an index from 0. That is an array.
2A one-dimensional array is one index
String[] seats = new String[3]; makes three slots, each starting as null. seats[0] = "A12" fills the first.
A for loop from 0 to seats.length - 1 walks every seat. The type can be int, Ticket, or any other type — the index rule does not change.
Figure. String[] seats = new String[3] is three slots. seats[0] is A12, seats[1] is A13, seats[2] is A14. The printed index is 1 — the second seat.
Index 1 is the second seat
String[] seats = new String[3];
seats[0] = "A12";
seats[1] = "A13";
seats[2] = "A14";
System.out.println(seats[1]);Read the middle seat
seats holds A12, A13, A14 at 0, 1, 2. What is seats[1]?
- seats[0]A12
- seats[1]A13
- seats[2]A14
Pro tip. The number in the brackets is the index, not the seat name.
Coding lab. Fill three seats and print the middle runs in the app, with checks on your output.
3A two-dimensional array is a row of rows
int[][] hall = new int[2][3]; is two rows, three seats each. hall[r][c] is the seat in row r, column c.
Java stores this as an array of arrays. hall[0] is the first row, itself a one-dimensional array.
Figure. Two rows of three. hall[0][1] is the top-middle seat. Not drawn to the scale of a real hall.
Row 0, column 1 holds 200
int[][] hall = new int[2][3];
hall[0][1] = 200;
System.out.println(hall[0].length);Price in row 0, column 1
hall is new int[2][3]. Then hall[0][1] = 200. What is hall[0][1], and how long is hall[0]?
- hall.length2 rows
- hall[0].length3 seats
- hall[0][1]200
Pro tip. hall[0] is the first row array. The second index picks a seat in that row.
4A three-dimensional array is a stack of grids
A 3D array is an array of 2D arrays. int[][][] multiplex = new int[2][2][3]; can mean two screens, each a 2-by-3 hall.
multiplex[s][r][c] is screen, then row, then column. You still walk with three nested loops. Most desk work stops at 2D.
Figure. A 3D array is an array of 2D arrays. multiplex[s][r][c] is screen, then row, then column. multiplex[1][0][2] holds 180.
How it works
- A three-dimensional array is a stack of grids3D is a grid per slice. Read the indices in the order you declared them.
Screen 1, row 0, column 2
int[][][] multiplex = new int[2][2][3];
multiplex[1][0][2] = 180;5Sort the row, then search it
A linear search walks index 0, 1, 2, … until the seat matches or the row ends. It does not need order.
A binary search jumps to the middle of a sorted row and throws away half each time. If the row is not sorted, the answer is not merely slow — it is not defined.
Figure. A linear search walks index 0, 1, 2 until the seat matches. A binary search jumps to the middle of a sorted row and throws away half each time. If the row is not sorted, the answer is not defined.
Linear search for A14
seats is A12, A13, A14. Find A14 by walking from 0.
- i = 0; seats[0] is A12not A14, continue
- i = 1; seats[1] is A13not A14, continue
- i = 2; seats[2] is A14found at 2
Pro tip. Linear search is the honest default when the row is short or unsorted.
6java.util.Arrays is the helper
The Arrays class in java.util holds static helpers for one-dimensional arrays: sort, binarySearch, fill, equals, toString.
Arrays.sort(prices) rearranges the same array. Arrays.binarySearch(prices, 200) needs that sort to have happened.
Figure. Arrays.sort rearranges the same array: 220, 180, 200. Arrays.binarySearch for 200 needs that sort to have happened.
How it works
- java.util.Arrays is the helperArrays.sort changes the array in place. binarySearch on that result needs the sort.
sort, then binarySearch, then print
import java.util.Arrays;
int[] prices = {220, 180, 200};
Arrays.sort(prices);
int at = Arrays.binarySearch(prices, 200);
System.out.println(Arrays.toString(prices));Notes
- In Object-Oriented Programming through Java because a hall is not one seat — it is a numbered row, then a grid, and Java's Arrays class sorts and searches that row.
- Index 0 is the first seat. length is the count. A 2D array is an array of rows. A 3D array is an array of those grids.
- Arrays.sort and Arrays.binarySearch live on java.util.Arrays. binarySearch needs a sorted array.
Exam traps & shortcuts
- seats[seats.length] does not exist. The last index is length - 1.
- binarySearch on an unsorted row is not a slower search — it is a wrong answer.
Recap
An array is a fixed row of one type. 2D is rows of rows. Arrays.sort and binarySearch are helpers, not magic.
- index from 0
- The last slot is length - 1. There is no seats[length].
- 2D is hall[r][c]
- hall[r] is one row. 3D adds one more index.
- sort before binary search
- Unsorted binarySearch is a wrong answer, not a slow one.
Practise Arrays
Reading is free and needs no account. Practice, mocks and progress live in the app.
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device