Array in Java

Advertisements

Java Array

Array is a collection of similar data type. A single variable can hold only one value at a time, If we want a variable to store more than one value of same type we use array. Array is linear data structure. It can store fixed number of values.


	

Address of first element is random, address of next element depend upon the type of array. Here, the type is integer and integer takes four bytes in memory, therefore every next address will increment by four.

Index of array will always starts with zero.

In java all elements are initialized to 0 by default.

As we know, Java allocates memory at runtime, therefore we can initialize the size of array at runtime by using new operator.

Declaration of Array

Syntax of declaration of array


Data-Type[] Array-Name = new Data-Type [ size ];

Example of declaration of array


	int[] array = new int[5];

In the above example we have declared an integer array which will hold five values. When we declare an array, sequential bolck of memory is allocated to hold the values and an array is initialized with a default value, which is "0".

Initialization of Array

Initialization means assigning value to declared Array.

Example 1 : initialization of array


	int[] array = new int[]{ 78, 45, 12, 89, 56 };

In the above example we have declared and initialized an array at same time. When we declare and initialize an array at same time, giving the size of array is optional.

Example 2 : initialization of array


	//ArrayDemo.java

    import java.util.Scanner;

    class ArrayDemo
    {
        public static void main(String args[])
        {
            int size,i;
            int[] arr;                     // Array Declaration

            Scanner sc = new Scanner(System.in);

            System.out.print("\n\tEnter size of array : ");
            size = Integer.parseInt(sc.nextLine());

            arr = new int[size];      // Array Instantiation

            for(i=0;i < size;i++)
            {
                System.out.print("\n\tEnter any number : ");
                arr[i] = Integer.parseInt(sc.nextLine());           // Dynamic initialization of Array
            }

            System.out.print("\n\n\tDisplaying array.\n\t");
            for(i=0; i < size; i++)
                System.out.print(arr[i] + ", ");

            System.out.print("\n\n\tDisplaying array using length property.\n\t");
            for(i=0; i < arr.length; i++)
                System.out.print(arr[i] + ", ");

        }
    }

   Output :

                Enter size of array : 5
                Enter any number : 78
                Enter any number : 45
                Enter any number : 12
                Enter any number : 89
                Enter any number : 56

                Display values, size given by user.
                78, 45, 12, 89, 56,

                Display values, size taken from built-in length() function.
                78, 45, 12, 89, 56,

Displaying array using enhanced for loop

Enhanced for loop is especially designed to travel each element of an array, without explicitly specify the size of array.

Syntax of java enhance loop


	for(data-type var-name : array-name)
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}

Example of java enhance loop


	//EnhancedForLoop.java

	import java.util.Scanner;

	class EnhancedForLoop
	{
	     public static void main(String args[])
	     {

	     int[] arr = new int[]{78, 45, 12, 89, 56};

	     System.out.print("\n\n\tDisplay values using enhanced for loop.\n\t");
	     for(int a : arr)
		     System.out.print( a + ", ");

	     }
	}

	Output :

				Display values using enhanced for loop.
				78, 45, 12, 89, 56,

Advertisement