Lesson 14 of 34

Arrays: Many Values Together

9 min 25 XP

An array stores many values of the same type in order. Indexes start at 0, and arr.length gives the number of elements. An array's size can't change once it is created.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

To fill an array from input: read the count n first, create new int[n], then read each value in a loop. Coding tests use this format all the time.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

arr[arr.length] throws ArrayIndexOutOfBoundsException. The last index is arr.length - 1.

Challenge

+25 XP

Read n and then n numbers. Print the largest and smallest. Input: 5 3 8 1 9 4 Output: Max: 9 Min: 1

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.