codegurukool.com · Free coding tutorials & online compiler

Home › Java Tutorial › Java arrays

Java arrays

A fixed-size list of one type.

An array has a fixed length and zero-based indexes. Read the size with length (no brackets). The Arrays class (import java.util.Arrays) offers sort and toString. A 2D array is an array of arrays.

Example code

import java.util.Arrays;

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

Run this code in the online editor

← Java break and continueJava methods →