Arrays

  • An array is a collection in java with similar types of elements that have a contiguous memory location.
  • In addition, special treatment is given to arrays in java as it is treated as an object which contains elements of a similar data type.
  • In addition, it is efficient to retrieve or sort the data with arrays and to access elements randomly.
  • It can store only a fixed set of elements. Therefore, it doesn’t grow its size at runtime. So we use the collection framework which grows automatically.
  • An array can contain primitives (int, char, etc) as well as to object (or non-primitive) elements.
  • Java array inherits the Object class and implements the Serializable as well as Cloneable interfaces.
  • Arrays in java can be classified as –
    • Single Dimensional Array
      • dataType[] a = {};
      • dataType[] a = new dataType[size];
      • dataType []a = {};
      • dataType []a = new dataType[size];
      • dataType a[] = {}; 
      • dataType a[] = new dataType[size];   
    • Multi Dimensional Array
      • dataType[][] a = {{},{},{}};
      • dataType[][] a = new dataType[row size][column size];
      • dataType [][]a = {{},{},{}};
      • dataType [][]a = new dataType[row size][column size];
      • dataType a[][] = {{},{},{}};
      • dataType a[][] = new dataType[row size][column size];
      • dataType []a[] = {{},{},{}};
      • dataType []a[]= new dataType[row size][column size];
  • ArrayIndexOutOfBoundsException –
    • JVM throws this exception if we try to access an element outside the array size.
    •  In other words, it indicates that an array has been accessed with an illegal index.
    •  An illegal index refers to one that is either negative or greater than or equal to the size of an array.

Scroll to Top