Lesson 17 of 34 · Arrays & ArrayList
Boss: The Dragon's Hoard (Java Arrays & ArrayList)
Ignaroth the Hoard Wyrm blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
Deep beneath the Highlands, Ignaroth the Hoard Wyrm sleeps on a mountain of gold. To slip past its jaws you must count, rank and catalogue the treasure without a single mistake — every wrong number makes the dragon stir.
Battle plan
This fight uses everything from Chapter 3:
- Array loops — totals with for-each, positions with an index loop.
- Sorting —
Arrays.sorton anint[]only sorts ascending. Sort a copy (so the caller's array stays untouched) and then read it backwards into a new array. ArrayList—containstells you whether an item is already in a list.- 2D arrays — the dragon's map is a
char[][]where'G'marks gold,'#'rock and'.'empty floor. Rows can have different lengths.charvalues are primitives, so compare them with==.
Assignment
Write five static methods in Main:
totalGold(int[] piles)— the total of all piles; an empty hoard has0gold.richestPile(int[] piles)— the index of the largest pile. If several piles share the maximum, return the first one. Return-1for an empty array.sortedDescending(int[] piles)— a new array with the piles from largest to smallest. The input array must not change.uniqueItems(List<String> loot)— a new list without duplicates, keeping each item's first appearance in its original order. The input list must not change.countGold(char[][] map)— how many cells contain'G'.
int[] piles = {120, 45, 300, 45};
totalGold(piles) // 510
richestPile(piles) // 2
sortedDescending(piles) // [300, 120, 45, 45]
uniqueItems(List.of("ruby", "crown", "ruby", "sword", "crown")) // [ruby, crown, sword]
Each passing test is a step closer to the exit. Pass all ten before Ignaroth wakes up.
Premium lesson
Unlock the full Learn Java course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.
Lesson last updated on .