Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0079 Arrays length

Array size, arrayName.length, holds its length. 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); } } This program displays the following output: length of a1 is 10 length of a2 is 5 length of a3 is 4