Array: – An array is a group of contiguous or related data items that share a common name. for instance, we can define an array name salary to represent a set of salaries of a group of employees. A particular value is indicated by writing a number called index number or subscript in a square bracket after array name. for example, salary[10] represents the salary of ten employees. While the complete set of values is referred to as an array, the individual values are called element. Arrays can be of any variable type.

In java, there are two types of array can be. They are called one-dimensional array and two-dimensional array.

One-dimensional array: – A list of item can be given one variable name using only one subscript and such a variable is called single subscripted variable or one-dimensional array. Before using one dimensional array, we must be declared and created in the computer memory before they are used. Creation of an array involves three steps –

  1. Declaring the array
  2. Creating memory location and
  3. Initialization or putting values

Declaration of array: – Before using of array in java, user must declare array with their type and size. During declaration of array, only square bracket can be use without its dimension. Array in java may declare in two forms –

  1. <Data Type> <Array Name>[];
  2. <Data Type[]> <Array Name>;

Creation of array: – After declaring an array, we need to create it in the memory. Java allows us to create arrays using new operator only. Syntax of array creation is –

                <Array Name>=new <Data Type>[Size];

For example –

                N=new int[5];m=new float[10];

These lines create necessary memory location for the arrays n and m and designate then as int and float respectively.

Initialization or putting the values: – The final step is to put value into the array created. This process is known as initialization. This is done using the array subscript with the following syntax –

                <Array Name>[Subscript]=Value;

For example –

                N[0]=30;m[5]=2.5f;         //Initialize the variable

Note that java creates array starting with the subscript with the zero and ends with a value one less than the size specified. Unlike C, java protects arrays from overcome and underrun. Trying to access to array bound is boundaries will generate an error message. We can also initialize arrays automatically in the same way as the ordinary value when they are declared without declaration and creation. Syntax of declaration, creation and initialization of that type of array is –

                <Data Type> <Array Name>[]={List of values separated by comma sign};

Array length: – In java, all arrays stored in the allocated size in a variable named length. We can have obtained length of array size using length operator. Syntax of the length operator is –

                <Variable Name>=<Array Name>.Length;

Two-dimensional array: – A two-dimensional array used in java to store large amount of data in form of table. In the two-dimensional array, double subscript is created and its first subscript is called row subscript start from 0 to its maximum size -1 and second subscript called column start from 0 to its maximum size -1. The first index select row and second index select the column within the row.

Some as one-dimensional array, two-dimensional array can be use with their three step such as declaring the array, creating memory location and putting value in the memory location.

Advertisements