Core Java - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Array

Java Array

shape Description

Java Array, Array is nothing but collection of similar data types and it can be also known as static data structure why because the size of an array will be given at the time of its declaration. Array starts from zero. Following is the syntax to declare an array. [java]datatype[] identifier;[/java]

shape Conceptual Figure

Advantages and Disadvantages

shape Description

Array Types

shape Description

Java Array consist of two types as follows.

Single Dimensional Array

Following is the syntax to declare single dimensional array. [java]dataType[] arr;[/java] Following is an example which describes more about the single dimensional array. singledimentional.java [java]public class singledimentional { public static void main(String args[]){ int a[]=new int[5];//declaration and instantiation a[0]=100;//initialization a[1]=200; a[2]=870; a[3]=640; a[4]=850; //printing array for(int i=0;i<a.length;i++)//length is the property of array System.out.println(a[i]); } }[/java] Output Now compile the code, result will be as follows. [java] 100 200 870 640 850[/java]

Multi Dimensional Array

Following is the syntax to declare multi dimensional array. [java]dataType[][] arrayRefVar;[/java] Following is an example which describes more about the multi dimensional array. multidimensional.java [java]public class multidimensional { public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } }[/java] Output When compile the code, resutl will be as follows. [java] 1 2 3 2 4 5 4 4 5 [/java]

Primitive Array and Derived Array

shape Primitive Array

Primitive array is an array of primitive types. Primitive array is defined by using datatypes. Ex:int[] array1=new int[5]; The following is an example for the primitive array. [java] package com.spl.primitive; public class PrimitiveArray { public static void main(String[] args) { System.out.println("Program starts"); int[] array1;//declaration array1=new int[5];//initialization System.out.println("Array value="+array1); System.out.println("Array length="+array1.length); System.out.println("2nd element="+array1[1]); System.out.println("Using normal for loop printing the values"); for(int i=0;i<array1.length;i++) { System.out.println(i+"th element is"+array1[i]); } System.out.println("Using advanced for loop printing the values"); for(int k:array1) { System.out.println("element is"+k); } System.out.println("Program ends"); } } [/java] Output: [c] Program starts Array value=[I@19eelac Array length=5 2nd element=0 Using normal for loop printing the values 0th element is0 1th element is0 2th element is0 3th element is0 4th element is0 Using advanced for loop printing the values element is0 element is0 element is0 element is0 element is0 Program ends [/c]

shape Derived Array

Java Array of derived classes is known as derived array. This array is created by java types. Ex: Apple[] array2=new Apple[5]; The following is an example for the Derived array. [java] package com.spl.primitive; public class DerivedArray { public static void main(String[] args) { System.out.println("Program starts"); //Array of Apple type Apple[] array1;//declaration array1=new Apple[5]; //creating objects array1[0]=new Apple(); array1[1]=new Apple(); array1[2]=new Apple(); array1[3]=new Apple(); array1[4]=new Apple(); //you can override toString() method in Apple Class System.out.println("array1 value="+array1); System.out.println("Using normal for loop printing the values"); for(int i=0;i<array1.length;i++) { System.out.println(i+"th element is="+array1[i]); } System.out.println("Using advanced for loop printing the values"); for(Apple k:array1) { System.out.println("element is="+k); } System.out.println("Program ends"); } } class Apple { } [/java] Output: [c] Program starts array value1=[Lcom.spl.primitive.Apple;@ififba0 Using normal for loop printing the values 0th element is=com.spl.primitive.Apple@1befab0 1th element is=com.spl.primitive.Apple@13c5982 2th element is=com.spl.primitive.Apple@1186fab 3th element is=com.spl.primitive.Apple@14b7453 4th element is=com.spl.primitive.Apple@c21495 Using advanced for loop printing the values element is=com.spl.primitive.Apple@1befab0 element is=com.spl.primitive.Apple@13c5982 element is=com.spl.primitive.Apple@1186fab element is=com.spl.primitive.Apple@14b7453 element is=com.spl.primitive.Apple@c21495 Program ends [/c] In derived array, an instance of the class can be stored.Suppose, if a derived array of type Apple is created, different types of Apple can be stored in that Java Array. The following is an example for addition of 2 matrices in java. [java] package com; public class ArrayAddition { public static void main(String args[]){ //creating two matrices int a[][]={{2,3,4},{4,4,5}}; int b[][]={{1,5,4},{3,7,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j=0;j<3;j++){ c[i][j]=a[i][j]+b[i][j]; System.out.print(c[i][j]+" "); } System.out.println();//new line } }} [/java] Output: The result will be as follows. [java] 3 8 8 7 11 10 [/java]

Summary

shape Key Points

  • The disadvantage of arrays can be overcome by using collection API’s or framework.
  • The limitation of array is, it is of homogeneous type and every element is referred through index only and elements are not stored in the structured format and array size is restricted.