Java Array Length
In this chapter you will learn:
Description
You can refer to the length of the array - the number of elements it contains - using length, a data member of the array object.
Syntax
Array size, arrayName.length
, holds its length.
Example
The following code outputs the length of each
array by using its length
property.
public class Main {
public static void main(String args[]) {
int a1[] = new int[10];
int a2[] = {1, 2, 3, 4, 5};
int a3[] = {4, 3, 2, 1};
System.out.println("length of a1 is " + a1.length);
System.out.println("length of a2 is " + a2.length);
System.out.println("length of a3 is " + a3.length);
}// w w w. jav a 2s .c om
}
This program displays the following output:
Next chapter...
What you will learn in the next chapter:
- How to Initialize Java Arrays
- Syntax to Initialize Arrays
- Example - How to initialize an array during the declaration