Array length in Java

Source: Internet
Author: User

Previously, the array in Java was always unknown, especially the length problem in the array. Today, I have a small understanding of this problem, but I have a better understanding of Java arrays. Let's talk about it. Let's start with an example:

Eg code:

package com.test;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubfinal   int   TABLE_SIZE   =   5;         final   int   TABLE_SIZE1   =   6;         final   int   TABLE_SIZE2   =   7;         int[][][]   table1   =   new   int[TABLE_SIZE][TABLE_SIZE1][TABLE_SIZE2];         int[][]   table2   =   new   int[TABLE_SIZE][TABLE_SIZE1];        int[]   table3   =   new   int[TABLE_SIZE];         int   a   =table1[4][0].length;         int   b   =table2[2].length;        int   c   =table3.length;                 System.out.println(a);         System.out.println(b);         System.out.println(c); }}

What is the output result of this example?

I believe there is no problem with the one-dimensional array. Let's take a look at what the two-dimensional array is.

In Java, multi-dimensional arrays are considered as arrays of arrays. I did not understand this sentence very well (especially for cainiao ). Let's use the example. For example:

Int B = Table2 [2]. length;

The table_size1 value output by B. We know that Table2 is a two-dimensional array, and the result of the length method is the length of the array and the length of the array is retrieved at a time, that is, if it is an array, directly retrieve the length of an array, for example, int c = table3.length. The result of C is the length of an array table3, that is, table_size. If int
B = Table2 [2]. length; replace it with int B = table2.length. In this case, B Outputs the length of the First-dimensional array in the two-dimensional array, that is, table_size (equal to C)
And if it is int B = Table2 [2]. length, note that the length is the length of the second-dimensional array of the Two-dimensional array Table2, that is, table_size1, As long as Table2 [2]. the value x in this brackets is between 0 and the length of its first-dimensional array minus 1 (TABLE_SIZE-1), its Table2 [X]. the length is the length of the second-dimensional array, and so on. In this example, the value of a is table_size2.

The result in the example is:

7
6
5


 

 

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.