You are here:   HomeJava ProgrammingJava ArrayEnter Code, Name And Salary Of 3 Employees And Print Then, Using Array of Objects
by Dinesh Thakur Category: array

Array is a group of related data items, that share a common name with same data type. An individual variable in the array is called an array element.

We can define an array name rollno to represent a set of roll no of a group of students. A particular value is indicated by a number called index number. An index number is present in brackets after the array name.

 

To create an array, you need to perform three steps:

 

(i) Declare the array

(ii) Create memory space

(iii) Store data into the created memory.

 

One dimensional array

 

A list of items group in a single variable name with only one index is called 1-D array.

 

(i) Declaration of an array

 

We can declare an array injava using subscript operator. The general form is

 

datatype var_name[ ];

is equivalent to

datatype[ ] var_name[ ] ;

 

Here, datatype is valid java datatype and var_name is the name of the array.

 

Example

 

int number [ ] ;

int [ ] number;

 

(ii) Create memory space

 

After declaration of an array, we must allocate a memory space for the declared array. This is done with the help of new operator. The general form is:

 

array_name = new datatype[ size];

 

Example

 

number = new int[ 10 ];

avg = new float[10];

 

It is also possible to combine the declaration and creation into one.

 

The general form is

datatype arrayname[] = new datatype [size];

 

Example

 

int number[]=new int[10];

float avg[]=new float[ 10];

 

(iii) Initialization of arrays

 

We can store values at the time of declaration. The compiler allocates the required space depending upon the list of values. The general form is:

 

datatype array_name[] = { list of values};

Example of One dimensional array

class arraynote

{

                  public static void main(String args[])

          {

                  int days[]={30,28,29,31,30};

                  System.out.println("January has " +days[3]+ " days");

           }

}

 

class ArrayExample

{

              public static void main(String args[])

        {

              int days[]= new int[5];

              days[0]=28 ;

              days[1]=31;

              days[2]=30 ;

              days[3]=31;

              days[4]=30;

              System.out.println("January has " +days[1] + " days");

         }

}

class arraynote

{

           public static void main(String args[])

      {

           double arraynote[]=new double[3];

           arraynote [0]=22.58;

           arraynote [1]=60.48;

           arraynote [2]=32.58;

           System.out.println(arraynote[0]);

           System. out. println( arraynote[1]);

           System.out.println(arraynote[2]);

       }

}





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article