Java multi-dimensional array

Source: Internet
Author: User

We know that in Java, we can construct an array of objects as follows:

class A {
    int a;
    int b;
}
 
public class Test {
    public static void main(String[] args) {
A [] A = new A [2]; // each element in the array is
System. Out. println (A [0]); // output null
    }
}

We can see that square brackets after the data type can constitute an array composed of the data type. What if I want to add another dimension? Obviously, you only need to add square brackets to the right of the previous array. For example, if a one-dimensional array can be written as a [], then a two-dimensional array can be written as a [], but which of the two square brackets is the square brackets in a []? If we follow the above logic, it must be the one on the left. For example, a [2] [1] is an array composed of one a [2.

Is that true? Perform the following tests:

class A {
    int a;
    int b;
}
 
public class Test {
    public static void main(String[] args) {
        A[][] a = new A[2][1];
System. Out. println (A. Length); // The output result is 2.
    }
}

As shown in the preceding example, [] on the right is the square brackets in. That is, a [2] [1] represents an array composed of two A [1. The basic data type (such as int [] []) is also tested.

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.