Java instance program for obtaining the length of an array (two-dimensional array)

Source: Internet
Author: User

This article describes how to obtain the length of an array (one-dimensional array, two-dimensional array length) in java.

We may know that js has a length function, and java also has a length function.

Example

If the array is data [], data. length

The Code is as follows: Copy code

Byte [] phone = new byte [81]; // create a byte array with a length of 81
Phone [I]! = 0 phone [I]! // The I position of the array is not equal to 0.

For example:

The Code is as follows: Copy code

Byte [] phone = new byte [81]; // create a byte array with a length of 81
Phone [1]! = 0 phone [1]! // The second value of the array is not equal to 0.

The following is an example:

The Code is as follows: Copy code

Public class StudyArrary {

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Byte [] phone = new byte [81]; // create a byte array with a length of 81
For (int a = 0; a <81; a ++)
Phone [a] = (byte) a; // The I position of the array is not equal to 0.

For (int I = 0; I <81; I ++ ){
If (phone [I]! = 0 ){
System. out. println ("phone [" + I + "] not equal to 0" + ", phone" + I + "is" + phone [I]);
}
Else
System. out. println ("phone [" + I + "] equals 0" + ", phone" + I + "is" + phone [I]);
}
}

}

Length of a two-dimensional array

The Code is as follows: Copy code
Public class Lesson {
Public static void main (String [] args ){
 
// Two-dimensional array declaration method:
// Data type [] [] array name = new data type [length] [length];
// Data type [] [] array name = {123}, {456 }};


Int [] [] num = new int [3] [3]; // defines a two-dimensional array with three rows and three columns.
Num [0] [0] = 1; // assign a value to the first element of the first row
Num [0] [1] = 2; // assign a value to the second element in the first row
Num [0] [2] = 3; // assign a value to the third element in the first row

Num [1] [0] = 4; // assign a value to the first element of the second row
Num [1] [1] = 5; // assign a value to the second element in the second row
Num [1] [2] = 6; // assign a value to the third element in the second row

Num [2] [0] = 7; // assign a value to the first element of the third row
Num [2] [1] = 8; // assign a value to the second element in the third row
Num [2] [2] = 9; // assign a value to the third element in the third row
For (int x = 0; x
For (int y = 0; y
System. out. print (num [x] [y]);
}
System. out. println ("/n ");
}
}
}

// Array value arr [x] [y] indicates the value of column y in row x.
// When using a two-dimensional array object, pay attention to the length represented by length,
// Add length (such as arr. length) directly after the array name, which refers to rows );
// Specify the index with length (for example, arr [0]. length), which refers to the element owned by the row, that is, the number of columns.

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.