Lesson 4 of 20

Arrays

Arrays

An array stores a fixed-size sequence of elements of the same type:

int[] nums = {5, 2, 8, 1, 9};
String[] names = {"Alice", "Bob", "Charlie"};
double[] prices = new double[3];  // filled with 0.0

Access elements by index (0-based):

nums[0]   // 5
nums[4]   // 9
nums.length  // 5 (not a method call — it's a field)

java.util.Arrays

The Arrays utility class provides sorting and printing:

import java.util.Arrays;

int[] arr = {3, 1, 4, 1, 5};
Arrays.sort(arr);                  // sorts in place
System.out.println(Arrays.toString(arr));  // [1, 1, 3, 4, 5]

2D Arrays

int[][] grid = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(grid[1][0]);  // 3

Your Task

Given nums = {5, 2, 8, 1, 9, 3}:

  1. Print its length
  2. Print the first element
  3. Sort it, then print Arrays.toString result
  4. Print the first element after sorting
TeaVM (WASM) loading...
Loading...
Click "Run" to execute your code.