Skip to content
Codewilds

Lesson 17 of 34 · Arrays & ArrayList

Boss: The Dragon's Hoard (Java Arrays & ArrayList)

Boss battlehard100 XP1 min read

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.sort on an int[] only sorts ascending. Sort a copy (so the caller's array stays untouched) and then read it backwards into a new array.
  • ArrayList — contains tells 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. char values are primitives, so compare them with ==.

Assignment

Write five static methods in Main:

  1. totalGold(int[] piles) — the total of all piles; an empty hoard has 0 gold.
  2. richestPile(int[] piles) — the index of the largest pile. If several piles share the maximum, return the first one. Return -1 for an empty array.
  3. sortedDescending(int[] piles) — a new array with the piles from largest to smallest. The input array must not change.
  4. 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.
  5. 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
Play the 8 free lessons

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 .

Write code ↓
Java+100 XP

Lesson 17 is part of the full course. The first 8 lessons are free.

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
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.