Java Array In Details - FindersAdda : Tutorials of Make Money, Computer or Internet

Monday, 20 November 2017

Java Array In Details


An array is an indexed collection of fixed number of homogeneous data elements.

An array shares common name for all its elements. Array works on index an index number and index begin from 0 and goes to size-1.

The main advantages of array is that we can store or represent huge number of value by using single variable. So that readability of the code will be improved.

The main disadvantages of array is that fixed in size i,e once we creates an array there is no chance of increasing or decreasing the size based on our requirement. Hence to use arrays concept compulsory we should know the size in advanced, which may not possible always.

Characteristics of an Array


1. It stores homogeneous or similar elements.

2. All the elements of an array shares the common name for all its element and they are distinguished from one another through the index number.

3. Any particular element of an array can be modified separately without disturbing other elements.

4. Index number plays an important role for calling each element of an array.

5. An arrays elements can be assigned to another ordinary variable.

6. The arrays elements are stored in continuous memory location.


Types of an Array


There are following types of an array.
(1) Single Dimensional Array
(2) Multi Dimensional Array

  1. Read Also : What Is Java and features of java?
  2. Read Also : Literals In Java.
  3. Read Also : Data Types In Java.


Single Dimensional Array


An array which is declared with only one index is known as single dimensional array. It is also known as One dimensional array. It is used in Searching and Sorting.

Declaration of Single Dimensional Array


Syntax : int[] x; 

                       int []x;
                   int x[];
All declarations are true because JVM reduce space at the time of execution.
The above syntax is recommended because name is clearly separated from type.

At the time of declaration, we can't specify the size. If we are trying to specify the size during the array declaration then we will get compile time error.


Creation of single Dimensional Array


Every array in java is an object. Hence, we can create arrays by using new operator.

Syntax : int[] x = new int[size];

For every array type, corresponding classes are available and these classes are part of java language and not available to the programmer level.

class First
{    
      public static void main(String[] args)     
        {          
                int[] x = new int[5];          
                System.out.println(x.getClass().getName());     
        }
}

output of above program



Explanation : In the above program, we are trying to print class name for single dimensional integer data type. And we get output like [I.


Some Points That Tell About Array Creation


1).. At the time of array creation, size declaration is compulsory. If we don't declare size of an array during the array creation then we will get compile time error.

int[] x = new int[5];

2).. It is possible to declare size with 0 in Java.

int[] x = new int[0]





3).. If we are trying to specify the array size with some negative int value then we will get RunTimeException : Negative Array Size Exception.

int[] x = new int[-4];

4).. To specify array size, the allowed data types are byte, short, char, int. If we are trying to specify another type then we will get compile time error.

  • int[] x = new int[20];
  • int[] x = new int['a'];
  • byte b= 20;
       int[] x = new int[b];
  • short l  = 30;
      int[] x = new int[l];



The above all array creations are true.

NOTE : The maximum allowed array size in java is 2147483647 which is the maximum value of int data type.

int[] x = new int[2147483647];

If sufficient memory is not available in your computer then you may get run time exception.

Array Initialization of Single Dimensional Array 


Once we create an array then every array element by default initialized with default value. If we are not satisfied with default value then we can override these value with our customize value.

Whenever, we are trying to print any reference variable then toString() method internally called, which is implemented by default to return the string in the following form.

Syntax : class-name @ hash-code_in_hexadecimal-form

If  we are trying to access array element with out of range index (either positive or negative int value) then we will get run time exception : ArrayIndexOutOfBoundsException.

We can initialize single dimensional array in two ways such as : Compile time initialization and run time initialization.


class First
{    
       public static void main(String[] args)     
        {            
                int[] x = new int[5];            
                System.out.println(x[0]);            
                System.out.println(x);     
        }
}


Output of above program



Explanation : In the above program, we are trying to print default value of x[0] and reference of x. Then we are getting output 0 and [I@15db9742.



Compile Time initialization



class First
{    
       public static void main(String[] args)      
        {          
               //First way             
                   int[] x = new int[2];            
                   x[0] = 5;            
                   x[1] = 10;
         
               //Second Way            
                   int[] y = {130,7,5,8,2};
        
                  System.out.println(x[0]);            
                  System.out.println(y[0]);     
       }
}

Output of above program


Explanation : In the above program, we have initialized the value of variable at compile time in two ways.




  1. Read Also : Some Queries For Performing Oracle Database.

Multi-Dimensional Array


An array which is declared with two index is known as multi-dimensional array.

In java, multi-dimensional array is not implemented by matrix style. Some people followed array of arrays approach for multidimensional array creation.

multidimensional internal structure



The main advantages of this approach is memory utilization will be improved.



Declaration of Multidimensional array



int[][]        x;
int        [][]x;
int        x[][];
int[]        []x;
int[]        x[];
int        []x[];

The above all declarations are true because JVM reduce space at the time of execution.



Multidimensional array Creation


int[][] x = new int[2][3];

The above declaration is used for storing same element in all one dimensional array.

int[][] x = new int[2][];
x[0] = new int[3];
x[1] = new int[4];

The above declaration is used for storing different element in all one dimensional array.



Valid Array Creation
Invalid Array Creation
int[][] x = new int[3][];
int[][] x = new int[][];
int[][] x = new int[3][4];
int[][] x = new int[][4];
int[][][] x = new int[3][4][];
int[][][] x = new int[3][][5];
int[][][] x = new int[3][4][5];
int[][][] x = new int[][4][5];


Multidimensional Array Initialization


class First
{     
       public static void main(String[] args)       
        {   
        
                int[][] x = new int[2][];            
                System.out.println(x);            
                System.out.println(x[0]);            
                System.out.println(x[0][0]);       
        }
}


output of above program


Explanation : In the above program, we have created multidimensional array but we have declare only one dimensional array and we are trying to print x[0] and x[0][0] then we are getting output null and Run Time Exception.



Some Array Type and Its Corresponding Class Name List

Array Type
Corresponding Class Name
int[]
[I
int[][]
[[I
double[]
[D
short[]
[S
byte[]
[B
boolean[]
[Z




Final Words


I hope you liked this post so much. If you loved this post, Then, share with your friends and also give some suggestion that what should i improve in this post. Thank You So Much...... 

About Author

SUMIT KUMAR GUPTA
SUMIT KUMAR GUPTA

Author & Editor

Myself Sumit Kumar Gupta & 21 year's old person. I am a programmer and blogger. I spend much more time on programming and blogging and helps other programmers and bloggers.

0 comments:

Post a Comment

We are eager to see your comments.
Comment Rule :

1. Don't use any link in the comment box. If you have any problem related to link then contact me with the contact form.

2. Don't use the wrong word if you write the wrong word in comment box then it will get spam.