Array in java



An array is an group of contiguous or related data items that share a common name. the elements of an array are stored in contiguous memory locations and each individual element can be accessed using one or more indices or subscripts. A subscript or an index is a positive integer value , which indicates the position of an array. Arrays are used when a programmer wants to store multiple data items of the same type into a single list and also wants to access and manipulate individual elements of list.
Types of array
1)           One dimensional arrays
2)            Two dimensional arrays

One dimensional arrays:- A list of items can be given one variable name using only one subscript and such a variable is called a single –subscripted variable or a one-dimensional array.
The syntax for declaring a single-dimensional array is as follows :
data_type array_name[];
after an array is declared , you need to create it by allocating space to it in memory. Arrays are created using new operator.
Array_name=new data_type[size];

Example of array One dimensional arrays:-
int marks[];
marks= new int[5];
For example if we want to represent a set of five numbers, say (20,22,26,85,77) by an array variable number, then we may create  the variable number as follows
Int number[]={20,22,26,85,77};
The element will be store as follows
number [0]=20
number [1]=22
number [2]=26
number [3]=85
number [4]=77

Two dimensional arrays:- Two-dimensional arrays are useful when thedata being processed are to be arranged in the form of rows and columns(matrix form)
The syntax for declaring a two-dimensional array is as follows:
                data_type array_name[][];
The syntax for creating a two-dimensional array is as follows:
data_type array_name[row_size][column_size];
For example an array arr [][] of type int having three rows and two columns can be declared and created using this statement
Int arr[][]=new int[3][2];

Initialization of two dimensional array
                Int arr[3][2]={ {101,23},{56,55},{88,63} };
                        The element will be stored in  two dimensional array as follows:
arr[0][0] = 101
arr[0][1]  = 23
arr[1][0]  =56
arr[1][1] = 55
arr[2][0]  =88
arr[2][1] = 63

LinkWithin

Related Posts Plugin for WordPress, Blogger...