Creating multidimensional arrays in Java

Source: Internet
Author: User
Tags arrays rand

You can easily create multidimensional arrays in Java:
 

: Multidimarray.java//creating multidimensional arrays.

Import java.util.*;
  public class Multidimarray {static Random rand = new Random ();
  static int prand (int mod) {return Math.Abs (Rand.nextint ())% mod + 1;
    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 +) PRT ("a1[" + i + "] [" + j +]
    "] =" + a1[i][j]);
    -----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++) PRT ("a2[" + i + "] [" + j + "] [" + K + "] =" + a2[i][j][k]);
    A-varied-length array with vectors:int[][][] a3 = new Int[prand (7)][][];
      for (int i = 0; i < a3.length i++) {A3[i] = new Int[prand (5) []; for (Int J = 0; J < A3[i].length;
    J + +) A3[i][j] = new Int[prand (5)]; for (int i = 0; i < a3.length. i++) for (int j = 0; J < A3[i].length; J +) for (int k = 0; K < a
            3[i][j].length;
    k++) PRT ("a3[" + i + "] [" + j + "] [" + K + "] =" + a3[i][j][k]);  Array of non-primitive 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 +) PRT ("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 +) PRT ("a5[" + i +"[" + j + "] =" + a5[i][j]);
  } static void Prt (String s) {System.out.println (s); }
} ///:~


Length is used in the code used for printing, so it does not have to rely on a fixed array size.
The first example shows a multidimensional array of basic data types. We can use curly braces to set the bounds of each vector within the group:

int[][] A1 = {
{1, 2, 3,},
{4, 5, 6,},
};

Each square bracket pair moves us to the next level of the array.
The second example shows a three-dimensional array assigned with new. Here, the entire array is allocated immediately:
int[][][] A2 = new Int[2][2][4];
But the third example reveals that each vector that makes up a matrix can have any length:

    int[][][] a3 = new Int[prand (7)][][];
    for (int i = 0; i < a3.length i++) {
      A3[i] = new Int[prand (5) [];
      for (int j = 0; J < A3[i].length; j +)
        a3[i][j] = new Int[prand (5)];
    }


For an array created by the first new, the length of its first element is random and the length of the other elements is undefined. The second new in the For loop fills in the element, but maintains the indeterminate state of the third index-until it encounters a third new.
According to the output, you can see that if the initialization value is not explicitly specified, the array value is automatically initialized to 0.
You can use similar tables to handle arrays of non-base type objects. As you can see from the fourth example, it shows us the ability to collect multiple new expressions with curly braces:

    Integer[][] a4 = {
      new integer (1), new Integer (2)},
      {new Integer (3), new Integer (4)},
      {new Integer (5), new Integer (6)},
    };


The fifth example shows how to gradually build an array of objects of a non basic type:

    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);
    }


I*j just put an interesting value in the integer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.