← Java from Scratch Lesson 21 of 88 · 10 XP

Arrays

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

int[] scores = {10, 25, 7};
String[] names = new String[3];       // three nulls
int[] zeros = new int[5];             // five zeros
scores[0] = 15;
scores.length                         // 3 — a field, no parentheses
int[][] grid = new int[3][4];         // two-dimensional: grid[y][x]

Going out of bounds throws an ArrayIndexOutOfBoundsException.

The java.util.Arrays class:

Arrays.sort(scores);                  // in ascending order
Arrays.toString(scores)               // "[7, 15, 25]"
Arrays.fill(zeros, -1);
int[] copy = Arrays.copyOf(scores, 5);

An array is a reference type: int[] b = a; is the same memory, not a copy.

Task. The first number is n, followed by n integers. Print the array in ascending order in the Arrays.toString format, then the maximum and the average with one decimal place (%.1f).

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.