Arrays in java
Arrays :
An array is a group of contiguous are related data items that shares a common name
(Or)
Collection of homogenous data values share with common name
Syntax
Arrayname[size];
Ex :
Salary[10];
In this example we can define an arrayname salary to represent a set of salaries of a group of employees
Creating an array :
Like any other variable arrays must be declared and created in the computer memory before they are used creation of an array involves three steps
- Declaring an array
- Creating memory location
- Putting values into the memory location
1) Declaration of an array :
Declaration of an array in two Forms
1) type arrayname[];
Ex : float salary[];
2) type [] arrayname;
Ex : int[] stydentID;
2)Creation of an array :
After declaring an array we need to create it in the memory. Bye using new operator only we can create an array is shown below.
Array name = new type[size];
Ex : Number = int[5];
It is also possible to combine two steps
1) Declaration
2) Creation
Into a single instruction
Int number[] = new int [5];
Initialisation of array:
The final step is to put values into the array created. This process is known as initialisation.
Array name[subscript] = value
Ex :
Int number[5]
Number[0] = 21
Number[1] = 25
Number[2] = 30
Number[3] = 40
Number[4] = 50
Int Number [5] = {21,25,30,40,50}
One Dimensional array :
A list of items can be given one variable name using only one subscript and such a variables is called single subscripted variable ar one dimensional array.
Ex : int number[5]
number[0] = 2
Two Dimensional Array :
If the array variable using two subscripts such a variable is called two dimensional array normally to store tabular data we use two dimensional array
Ex :
int table[rowsize][colsize];
int matrix[row][col];
0 Comments