An Example of a Ragged Array - Java Language Basics

Java examples for Language Basics:Array

Description

An Example of a Ragged Array

Demo Code

public class Main {
  public static void main(String[] args) {  
    // Create a two-dimensional array of 3 rows
    int[][] raggedArr = new int[3][];

    // Add 2 columns to the first row
    raggedArr[0] = new int[2];

    // Add 1 column to the second row
    raggedArr[1] = new int[1];

    // Add 3 columns to the third row
    raggedArr[2] = new int[3];

    // Assign values to all elements of raggedArr
    raggedArr[0][0] = 1;//from  w  w  w . j  av a  2s. c o m
    raggedArr[0][1] = 2;
    raggedArr[1][0] = 3;
    raggedArr[2][0] = 4;
    raggedArr[2][1] = 5;
    raggedArr[2][2] = 6;

    // Print all elements. One row at one line
    System.out.println(raggedArr[0][0] + "\t" + raggedArr[0][1]);
    System.out.println(raggedArr[1][0]);
    System.out.println(raggedArr[2][0] + "\t" + raggedArr[2][1] + "\t" + raggedArr[2][2]);
  }
}

Result


Related Tutorials