Tsallaka zuwa abun ciki
Justice Tech School

Arrays

An array holds a fixed number of values of one type. Indexes start at 0. Use length for the size and the Arrays class for sort, toString and fill. Two dimensional arrays are arrays of arrays.

Example
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    int[] nums = {5, 2, 9, 1};
    System.out.println(nums.length + " items, first is " + nums[0]);
    Arrays.sort(nums);
    System.out.println(Arrays.toString(nums));
    int[][] grid = {{1, 2}, {3, 4}};
    System.out.println(grid[1][0]);
    String[] names = new String[2];
    System.out.println(names[0]);
  }
}

Result

4 items, first is 5
[1, 2, 5, 9]
3
null

Exercise

Get the number of items.

int n = nums.;

Quiz Test

Can an array change its size?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.