• An array is a collection of similar data types that share a common name.
  • Arrays of any type can be created and may have one or more dimensions.
  • Array elements can be accessed by its index.
  • Array index always starts with 0
  • It can be divided into two categories:
  1. One-dimensional Array
  2. Multi-dimensional Array

One-Dimensional Arrays

  • An array with a single sub script is referred as one-dimensional array.
  • The general form of a one-dimensional array declaration is:
          <data_type> <var-name>[];
          int num[];
  • The value of num is set to null, which represents an array with no value. To link num with an actual, physical array of integers, we must first allocate memory using new operator.
  • new is special operator that allocates memory.
  • The general form for memory allocation using new is as follows:
    <array-var> = new <data_type>[size];
  • Following example allocates a 3-element array of integers and assign it to num
    num = new int[3];
  • Declaring an array is a two step process.
  1. Declare a variable of the desired data type.
  2. Allocate memory that will hold the array, using new, and assign it to the array variable.
class arrayDemo
{
     public static void main(String args[])
     {
          int num[ ];
          num= new int[3];
          num[0]=10;
          num[1]=20;
          num[2]=30;
          System.out.println (“Value at index 1 is ” + num[1]);
     }
}
  • We can also combine the declaration of the array variable and memory allocation process in a single statement as shown below:
       int num[ ]=new int[3];
  • Arrays can also be initialized at the time of declaration using comma-separated list of expressions surrounded by curly braces.
  • The comma separates the values of the array elements.
  • It will automatically create an array with the size large enough to hold the number of elements specified in the array initialize list.
  • There is no need to use new.
Class AutoArray
{
     public static void main (String args[ ])
     {
         int month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
          System.out.println(“April has” + month_days[3] + “days.”);
     }
}

Multi-Dimensional Arrays:

  • Multidimensional arrays are actually arrays of arrays.
  • An array having two or more sub-script is known as multi-dimesional array
  • We can specify additional index subscript using another set of square brackets to declare a multidimensional array.
  • For example, following statement declares two dimensional array:
   int twoD[][] = new int [4] [5];
   int threeD[][] = new int[3][4][5];
  • The first statement declares array matrix with 4 rows and 5 columns
  • The second statement declares 3-D array of matrix with 4 rows and 5 columns

Alternative Array Declaration

  • There is an alternative form to declare an array:
<data_type> [ ] <var_name>;
  • The square brackets follow the type specifier, and not the name of the array variable.
  • For example, the following two declarations are equivalent:
int num[ ] = new int[4]; OR       int [ ] num = new int[4];
  • This declaration form can be helpful while declaring several arrays at the same time. For example,
int [ ] num1, num2, num3;