Tuesday 19 August 2014

ARRAYS IN JAVA



DEFINITION OF AN ARRAY :

An array is a group of similar data elements or it is a group of like-typed variables that are referred to by a common name.

For example, array of numbers like (1,2,4,8) these have same data type which is int.

Arrays of any type can be created and may have one or more dimensions.

Each item in an array (like 2 is one of the item in the above example) is called an element and each element can be accessed by its numerical index.

Note : Index in arrays is always starts from zero.

Consider the figure of array :


In the above diagram you can easily identify the difference between index and an element of array.

WHY WE USE ARRAYS :

This is the most common question for beginners in the programming that why we use arrays. 
You can understand this by considering the following example :

Suppose you want to use 100 or may be 1000 numbers which belongs to same type i.e. int ( may be to store the roll numbers of students ) than you want to create separate variables for each number you will use in your program if you don’t know about the arrays or the other alternative in that case you can use is that you can declare a 100 or 1000 number array which will save most of your time.

Declaration of an array :

 data_type[] name_of_array;  
Or
 data_type name_of_array[];  
Although the second form is only included as a convenience if you are coming from C/C++ but first form is preferred to declare arrays in java.

Initialization of an Array :

One way to create an array is with the new operator.

 name_of_array = new data_type[size_or_number_of_elements];  
   
 name_of_array[0] = 100; //Initialization of first element  
 name_of_array[1] = 200; //Initialization of second element  
   
The other way to initialize array is to directly set the elements. 
For example :

 int[] name_of_array = {1,5, 6, 23, 5, 2};  

One dimension arrays :

A one dimension array is, essentially, a list-typed variables like the one used in the above examples, they all are one dimensional arrays.

Example :
An array named myArray stores 5 numbers.

 int[] myArray; // array declared  
   
 myArray = new int[5]; // array allocates memory  
   

or you can combine these two steps into one step as :

 int[] myArray = new int[5];  

Now initialize the elements :

 myArray[0] = 4;  
   
 myArray[1] = 78;  
   
 .  
   
 .  

Or the other way to create this :



  int[] myArray = {4, 78, 23, 32, 1};  

Once the array is initialized you can access the elements by using the index which always starts from zero.

For example :
If you want to access element 3 than you can use the below statement to print the element 3 of an array.

 System.out.println("Element 3 is " + myArray[2]);  

Multidimensional Arrays :

Multidimensional arrays are actually array of arrays.

To declare a multidimensional array variable, specify each additional index using another set of square brackets.

For example :

  int [][] twoD = new int[3][4];  

Where the left dimension or first dimension specifies the number of rows and the other specifies the number of columns in the array.

A Program to make an array of 10 elements and print them on the screen is shown as an example :

1:  class ArrayDemo  
2:  {  
3:    public static void main(String[] args)  
4:    {  
5:      int[] myArray;  
6:      myArray = new int[10];  
7:    
8:      myArray[0] = 12; // remember the first element starts with zero index.  
9:      myArray[1] = 4;  
10:     myArray[2] = 6;  
11:     myArray[3] = 56;  
12:     myArray[4] = 34;  
13:     myArray[5] = 7;  
14:     myArray[6] = 7;  
15:     myArray[7] = 8;  
16:     myArray[8] = 23;  
17:     myArray[9] = 55;  
18:    
19:     //print the elements on the screen  
20:     for(int i=0; i<myArray.length; i++)  
21:     {  
22:        System.out.println("Element " + i+1 + ": " myArray[i]);  
23:     }  
24:    }  
25:  }  

3 comments :

  1. Very well explained about the array here. This kind of information helps us a lot understanding the programming concept easily. Thanks a lot.

    ReplyDelete
  2. The knowledge of technology you have been sharing through this post is very much helpful to develop new idea. here by i also want to share this.
    Dot Net Training in Chennai
    Software Testing Training in Chennai
    Java Training Institute in Chennai
    PHP Course in Chennai

    ReplyDelete