24su ver.
Note: this is for the Summer 2024 iteration of CSE 121. Looking for a different quarter? Please visit https://courses.cs.washington.edu/courses/cse121/.
Array - a list with a fixed size that contains values of the same type
Valid array examples
[0, 5, 12, 19]
["marvin", "photo", "1"]
Declaring Arrays¶
type[] name = new type[length];
type[] name = {VAL1, VAL2, VAL3, ...};
Array Methods¶
Using Arrays | Description |
---|---|
int value = name[i] | Get the value at index i |
name[i] = value | Set the value at index i |
name.length | Get the number of elements in name |
Common Array Patterns¶
Array Traversal
for (int i = 0; i < arr.length; i++) {
// do something with arr[i]
}
Array Traversal in Reverse
for (int i = arr.length - 1; i >= 0; i--) {
// do something with arr[i]
}