Accessing Elements of a Multi-dimensional Array - Java Language Basics

Java examples for Language Basics:Array

Description

Accessing Elements of a Multi-dimensional Array

Demo Code

public class Main {
  public static void main(String[] args){
    int[][] ra = new int[3][];    
    ra[0] = new int[2];
    ra[1] = new int[1];
    ra[2] = new int[3];
    //from  w  w  w. ja va  2s. c om
    // Populate the ragged array using for loops
    for(int i = 0; i < ra.length; i++) {
      for(int j = 0; j < ra[i].length; j++){
        ra[i][j] = i + j;
      }
    }

    // Print the array using for loops
    for(int i = 0; i < ra.length; i++) {
      for (int j = 0; j < ra[i].length; j++){
        System.out.print(ra[i][j] + "\t");
      }

      // Add a new line after each row is printed
      System.out.println();
    }    
  }
}

Result


Related Tutorials