Array in Java

Array in Java


     
      Array is a collection of similar type of elements that have contiguous memory location.Java array is an object the contains elements of same datatype. it is a data structure where we store similar elements. We can store only fixed set of elements in a java array.

Arrays in Java are homogeneous data structures implemented in Java as objects

Advantage of Java Array     :

1) Code Optimization : It makes the code optimized, we can retrieve or sort the data easily.

2) Random access       : We can get any data located at any index position 

 

Disadvantage of Java Array :

1) Size Limit : We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java.

 

There are two types of array.
  • Single Dimensional Array
  • Multidimensional Array

  1) Single Dimensional Array 

  //declaration and instantiation   

  int array[]=new int[5];

  array [0] = 100;    

  array [1] = 200

  array [2] = 700;

  array [3] = 400;   

  array [4] = 500;  

  for(int i=0; i<a.length ;++) {

     System.out.println("value is : "+array[i]);   

  }


2) Multidimensional java array

  int array[][]={{1,2,3,4},{2,4,5,6},{4,4,5,8}};

  for(int i=0; i<4; i++) {  

      for(int j=0; j<4; j++) {

           System.out.print(array  [i][j]+" "); 

      }   

      System.out.println();   

  } 

 

 

 

Comments

Popular posts from this blog

Java LinkedList class in Collection

Java ArrayList class in collections

Constructors in Java