Using jagged arrays - Java Language Basics

Java examples for Language Basics:Array

Introduction

The following code creates a jagged array.

public class Main {

  public static void main(String[] args) {
    String[][] teams = { { "H", "J" },
        { "B", "J", "J" },
        { "M", "F" },
        { "M", "R", "I" } };
  }

}

Enhanced for loop with jagged array

Demo Code

public class Main {

  public static void main(String[] args) {
    String[][] teams = { { "H", "J" },
        { "B", "J", "J" },
        { "M", "F" },
        { "M", "R", "I" } };


    for (String[] team : teams) {
      for (String player : team)
        System.out.println(player);
      System.out.println();//from  w w  w  . j a  va2 s  .com
    }

  }

}

Related Tutorials