10:58 pm - Friday March 22, 2019

Arrays in Java in java with Example Program

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044


Array is the static memory allocation. It allocates the memory for the same data type in sequence. It contains multiple values of same types.

It also stores the values in memory at the fixed size.

Multiple types of arrays are used in any programming language such as: one – dimensional, two – dimensional or can say multi – dimensional.

Declaration of an array:

int num[]; or

int num = new int[2];

Some times user declares an array and it’s size simultaneously. You may or may not be define the size in the declaration time. such as:

int num[] = {50,20,45,82,25,63};
  •         An array intializer may be any arbitrary expression.

int c = 15;

int[] primes = { 1, 2, 3, 5, 7, 9, 11 };

int[] a = { 1, primes[2], c, (int)Math.pow(2,2) };

  •       The first element in every array is the zeroth element.
  •    When memory is allocated for an array, the elements are automatically        initialized to theirdefault values: zero for all numeric primitive data types, false for boolean variables and null for references.
  •      As with all objects, if the array reference (object reference) is assigned a null value, then the garbage collector will reclaim the dereferenced (unused) memory.
  •        Arrays have a length data field (read only), specifying the number of elements in the array.

The length data field can be accessed through the dot operator.

for ( int k=0; k < a.length ; k++ )

System.out.println( a[ k ] );

One dimensional array:

Example :

public class DevArray
public static void main(String[] args)
double[] devList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: devList)

Two dimentional array:


Public class twoDimension
public static void main(String[] args)
int[][] a2= new int[10][5];
for(int i=0;i<a2.length;i++)
for (intj=0;j<a2[i].length;j++)
System.out.print(" " + a2[i][j]);


Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply