- 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:
- One-dimensional Array
- Multi-dimensional Array
- An array with a single sub script is referred as one-dimensional array.
- The general form of a one-dimensional array declaration is:
- Declare a variable of the desired data type.
- Allocate memory that will hold the array, using new, and assign it to the array variable.
public static void main(String args)
int num[ ];
num= new int;
System.out.println (“Value at index 1 is ” + num);
- 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;
- 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.
public static void main (String args[ ])
System.out.println(“April has” + month_days + “days.”);
- 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  ;
int threeD = new int;
- 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; OR int [ ] num = new int;
- This declaration form can be helpful while declaring several arrays at the same time. For example,
int [ ] num1, num2, num3;