One dimensional array in java

One dimensional array in java


Arrays are the collection of homogenous elements. In the sense they have same data type. Arrays can be declared as any dimensions.Each element of an array 
is accessed by it's index.

One dimensional array

It is a single list of same data type.The general form of an array is:
eg: type name[]
Here type indicate the data type of an array. Name indicates the name you wanna give to an array.Although you declared the array like this practically no array
exists, which represents the array with null value. In order to allocate the memory for this array one should declare it with new keyword.The syntax to allocate
memory for array is:
eg:name = new type [size];
Here type indicate the data type of array, size indicates number of elements in an array and name indicates the name of an array.

Let's write a program for array demonstration:

Code goes here:

import java.util.*;
class arraydemo
    public static void main(String args[])
        int[] array;
        int N;
        Scanner scan= new Scanner(;
        System.out.println("Enter the size of array");
        array=new int[N];       // memory allocation
        System.out.println("Enter the elements of array");
        for(int i=0;i<array.length;i++)
            array[i]=scan.nextInt();    //to scan elemnts of an array
        System.out.println("The elements are:");
        for(int i=0;i<array.length;i++)
            System.out.println(array[i]);    //To display the elements of an array

Link for online compiler(You can check the working of the program in this): click here.


Java The Complete Reference Eighth Edition 

No comments:

Powered by Blogger.