typestar

Arrays in Java

Fixed-size and primitive-friendly, with a utility class for the rest.

import java.util.Arrays;

int[] fib = { 1, 1, 2, 3, 5, 8, 13 };
System.out.println(fib.length + " values, third " + fib[2]);
System.out.println(Arrays.toString(fib));

// Arrays holds the utilities the array itself lacks
var middle = Arrays.copyOfRange(fib, 2, 5);
System.out.println(Arrays.toString(middle));
System.out.println(Arrays.binarySearch(fib, 8));

int[][] grid = new int[2][3];
grid[1][2] = 9;
System.out.println(grid[1][2]);

How it works

  1. Literal syntax initializes; length and [2] read.
  2. Arrays.toString, copyOfRange and binarySearch fill the gaps.
  3. new int[2][3] is an array of arrays, indexed twice.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
437
Tokens
144
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 96 seconds.

Type this snippet

Step 1 of 4 in Collections, step 11 of 29 in Language basics.

← Previous Next →

Arrays in other languages