Unit 6: Arrays
AP Computer Science AWhat you'll master in this unit
- •
- •
- •
- •
Arrays at a glance
java
int[] scores = {95, 87, 73, 91, 88};
// index: 0 1 2 3 4
System.out.println(scores[0]); // 95 (first element)
System.out.println(scores[4]); // 88 (last element)
System.out.println(scores.length); // 5 (number of elements)
Key facts about arrays
Two ways to traverse
java
// Standard for loop — when you need the index
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
// Enhanced for loop — when you just need each value
for (int value : arr) {
System.out.println(value);
}