Create multidimension arrays : Array « Collections Data Structure « Java






Create multidimension arrays

Create multidimension arrays
     
// : c04:MultiDimArray.java
// Creating multidimensional arrays.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.util.Random;

public class MultiDimArray {

  static Random rand = new Random();

  public static void main(String[] args) {
    int[][] a1 = { { 1, 2, 3, }, { 4, 5, 6, }, };
    for (int i = 0; i < a1.length; i++)
      for (int j = 0; j < a1[i].length; j++)
        System.out.println("a1[" + i + "][" + j + "] = " + a1[i][j]);
    // 3-D array with fixed length:
    int[][][] a2 = new int[2][2][4];
    for (int i = 0; i < a2.length; i++)
      for (int j = 0; j < a2[i].length; j++)
        for (int k = 0; k < a2[i][j].length; k++)
          System.out.println("a2[" + i + "][" + j + "][" + k + "] = "
              + a2[i][j][k]);
    // 3-D array with varied-length vectors:
    int[][][] a3 = new int[rand.nextInt(7)][][];
    for (int i = 0; i < a3.length; i++) {
      a3[i] = new int[rand.nextInt(5)][];
      for (int j = 0; j < a3[i].length; j++)
        a3[i][j] = new int[rand.nextInt(5)];
    }
    for (int i = 0; i < a3.length; i++)
      for (int j = 0; j < a3[i].length; j++)
        for (int k = 0; k < a3[i][j].length; k++)
          System.out.println("a3[" + i + "][" + j + "][" + k + "] = "
              + a3[i][j][k]);
    // Array of nonprimitive objects:
    Integer[][] a4 = { { new Integer(1), new Integer(2) },
        { new Integer(3), new Integer(4) },
        { new Integer(5), new Integer(6) }, };
    for (int i = 0; i < a4.length; i++)
      for (int j = 0; j < a4[i].length; j++)
        System.out.println("a4[" + i + "][" + j + "] = " + a4[i][j]);
    Integer[][] a5;
    a5 = new Integer[3][];
    for (int i = 0; i < a5.length; i++) {
      a5[i] = new Integer[3];
      for (int j = 0; j < a5[i].length; j++)
        a5[i][j] = new Integer(i * j);
    }
    for (int i = 0; i < a5.length; i++)
      for (int j = 0; j < a5[i].length; j++)
        System.out.println("a5[" + i + "][" + j + "] = " + a5[i][j]);
    // Output test
    int ln = 0;
    for (int i = 0; i < a3.length; i++)
      for (int j = 0; j < a3[i].length; j++)
        for (int k = 0; k < a3[i][j].length; k++)
          ln++;

  }
} ///:~



           
         
    
    
    
    
  








Related examples in the same category

1.Initialize a static array
2.Initialization and re-assignment of arraysInitialization and re-assignment of arrays
3.Doubling the size of an arrayDoubling the size of an array
4.Timing array loop performance
5.Array 2DArray 2D
6.Can you change the .length of an array
7.Show Two-Dimensional Array of Objects
8.ArrayListDemo done over using an ArrayList
9.Array Hunt game
10.Multi Dimension Array
11.Clone Array
12.Associates keys with valuesAssociates keys with values
13.Arrays of primitives
14.Creating arrays with new
15.Array initialization
16.Creating an array of nonprimitive objects
17.Initializing Array ValuesInitializing Array Values
18.Creating a Two-Dimensional Array
19.Initializing a Two Dimensional ArrayInitializing a Two Dimensional Array
20.Using the length VariableUsing the length Variable
21.Triangular array
22.Grow arrayGrow array
23.Define array for class Define array for class
24.String array and output to consoleString array and output to console
25.Multiply two matrices
26.Array Of Arrays Demo 2Array Of Arrays Demo 2
27.Array Copy DemoArray Copy Demo
28.Copying Elements from One Array to Another
29.Java program to demonstrate multidimensional arraysJava program to demonstrate multidimensional arrays
30.Extend the size of an array
31.Copy an array
32.Initialize multidimensional array
33.Get array upperbound
34.To get the number of dimensions
35.Resize an array, System.arraycopy()
36.Dump array content: Convert the array to a List and then convert to String
37.java.utils.Arrays provides ways to dump the content of an array.
38.Dump multi-dimensional arrays
39.Use the new shorthand notation to iterate through an array
40.Create a repeated sequence of character
41.Reverse array elements order
42.Convert array of primitives into array of objects
43.Array Initializers
44.Reinitializes a byte array
45.Reinitializes an int array
46.Sum all elements in the array
47.Sums an array of numbers log(x1)...log(xn)
48.A class to iterate over all permutations of an array.
49.Palidrome Array
50.Set of utilities used to manipulate arrays.
51.ArrayUtils provides static methods for manipulating arrays when using a tool such as java.util.ArrayList is inconvenient.
52.Array Util
53.clone two dimensional array