CSE 121
CSE 121
  • Home / Calendar
  • Syllabus
  • Assignments
  • Resubmissions
  • Exam
  • Course Staff
  • Office Hours
  • Grading Rubrics
  • Resources

  • Course Tools
  • EdStem
  • Anonymous Feedback
  • Acknowledgements

Arrays


24au ver.

Note: this is for the Autumn 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]
}

Search

Search the class website; related sections and pages will appear below. Note: this search is not as forgiving with typos as other search engines.