Free course · Lesson 3 of 5
Arrays & Basic Collections
In this lesson you will
- Store several values in an array and loop over them
- Use ArrayList and HashMap for data that grows
An array holds a fixed number of values of the same type. For lists that need to
grow or shrink, Java's Collections Framework provides ArrayList — flexible, but still
type-safe thanks to generics like ArrayList<String>.
int[] scores = {78, 91, 64};— fixed-size, fast, type-checked.ArrayList<String> names = new ArrayList<>();— resizable list.HashMap<String, Integer>stores key-value pairs, Java's equivalent of a dictionary.- The enhanced
forloop (for (String name : names)) reads cleanly.
💡 Practical Skill: Choosing the right collection — array, list, or map — is one of the most common real-world Java decisions, from processing form submissions to building reports.
import java.util.ArrayList;import java.util.HashMap; public class Main { public static void main(String[] args) { HashMap<String, Integer> scores = new HashMap<>(); scores.put("Amina", 78); scores.put("Deng", 91); scores.put("Grace", 64); ArrayList<String> passed = new ArrayList<>(); for (String name : scores.keySet()) { if (scores.get(name) >= 70) { passed.add(name); } } System.out.println("Passed: " + passed); }}
Practice exercise
Store seven daily temperatures in an array, then print the highest, the lowest and the average.
Key takeaways
- Array indexes start at 0 and end at length - 1
- ArrayList grows as you add items; HashMap finds values by key
- Most data tasks are a loop that totals, counts or finds a maximum
Free self-study lesson — no certificate. For the full instructor-led program, see registration options.