Core Java · Chapter 7 of 48
Arrays
By the end of this chapter you can keep many values in one array, walk through it, sort, copy and fill it with Arrays, and work with an array of rows. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: First steps
- 6 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
Ravi has yesterday's sales sheet: cups sold in each hour at SBC, NDLS and CSMT. He wants the day's total, the busiest hour, each station's total, and a sorted list of prices that does not wreck the menu's own order. Every morning the counters also open with the same stock.
With plain variables you would write hour1, hour2, hour3 and so on, one variable per hour and a line of code for each. A station that stays open one hour longer needs a new variable and new code, and you cannot loop over names you typed by hand.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Sales.java: add up the cups sold
In Sales.java, make total(cups) return all the cups in the array added up, using an enhanced for loop. An empty array returns 0.
For example, total of 12, 40, 31, 8 and 22 returns 113, an array with just 40 returns 40, and an empty array returns 0.
- Task 2
Sales.java: find the busiest hour of the day
In Sales.java, make busiestHour(cupsByHour) return the index, counting from 0, of the hour with the most cups. On a tie the earlier hour wins. An empty array returns -1.
For example, 12, 40, 31, 8, 22 returns 1; 1, 2, 3, 4, 5 returns 4; 5, 30, 12, 30 returns 1, not 3; a single hour returns 0; 0, 0 returns 0; an empty array returns -1.
- Task 3
Sales.java: sort a copy of the prices
In Sales.java, make sortedCopy(prices) return a new array with the same prices sorted smallest first. The array passed in must keep its order, and the result must be a new array, not the same one. An empty array returns an empty array.
For example, sortedCopy of 50, 25, 40, 10 returns 10, 25, 40, 50, and the array passed in is still 50, 25, 40, 10 afterwards.
- Task 4
Sales.java: fill the opening stock for every counter
In Sales.java, make openingStock(counters, cups) return a new array with one slot per counter, every slot holding the same number of cups. 0 counters returns an empty array.
For example, openingStock(3, 100) returns 100, 100, 100 and openingStock(1, 60) returns 60.
- Task 5
Sales.java: total each station's sales
In Sales.java, make stationTotals(sales) return a new array with one total per row of the 2D array, in row order: each row is one station, each value in it one hour's cups. Rows can have different lengths, an empty row totals 0, and no rows at all returns an empty array.
For example, rows 12, 40, 31 and 20, 55, 18 and 9, 14 return 83, 93, 23; an empty row and a row with 7 return 0, 7.
- Task 6 · interview question
Interview: Sales.java, predict == and Arrays.equals on two arrays
In Sales.java, make predictArrays() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (true or false in lower case, an array the way Arrays.toString shows it).
The check runs the same code and compares your list with what Java really printed.
What you will learn
- Arrays: a fixed number of values of one type, reached by index from 0.
- Looping over an array: the enhanced for for every value, an index when the position matters.
- The Arrays class: toString, sort, copyOf, fill and equals.
- Arrays are objects: a variable holds a reference, and a 2D array is an array of arrays.
More in First steps
Build "Arrays" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.