Array of Arrays Java

Array of Arrays: It is not new to a C/C++ programmer about array of arrays. He knows well that a 2D array is nothing but array of one-dimensional arrays. Of course, Java also means the same.

2D arrays are clearly discussed in: Two-Dimensional Arrays (Array of Arrays)

Anyhow, as another example on Array of Arrays, herewith given two-dimensional string array (array of string arrays).

public class ArrayOfStringArray
  public static void main(String args[])
    String ones[] = {"one",  "two", "three", "four", "five"};
    String tens[] = {"ten",  "twenty", "thirty", "forty", "fifty"};
    String hundreds[] = {"one hundred",  "two hundred", "three hundred", "four hundred", "five hundred"};
    String thousands[] = {"one thousand",  "two thousand", "three thousand", "four thousand", "five thousand"};

    String allInOne[] [] = {ones, tens, hundreds, thousands};

    System.out.println("Printing the elements::::");
    for(int i = 0; i < 4; i++)
      for(int j = 0; j < 5; j++)
        System.out.print(allInOne[i][j] + " ");
      System.out.println();                        // to give new line

Array of Arrays

A nested for loop is used to print the elements.

All array operations are available with All Array Operations at a Glance.

Leave a Comment

Your email address will not be published.