An array is a group of line-typed variables that are referred to by a common name. Arrays of any type can be reacted and may have one or more dimensions. A specific element in an array is accessed by its index. Arrays offered a convenient means of grouping related information.
Arrays are objects that contain a number of variables of the same type. These component variables are referenced using the integer indices 0,…, n-1, where n is the length of the array. The type of the array is identified by appending [] to the type of its components. For example, int[] identifies an array of type int, Object[] identifies an array of type Object, and char[][] identifies an array of an array of type char.
One-Dimensional Arrays
A one-dimensional array is essentially a list of like typed variables.
Creating an array
Like any other variables, arrays must be declared and created in the computer memory before they are used . The array creation involves following stapes.
1. Declaring the array
2. Creating memory locations
3. putting values into the memory locations
1.Declaring an array
type var-name[ ] ; or type[] var_name;
Here ,type declares the base type of the array. The base type determines the data type of each element that comprise the array.
Ex.
int x[ ];
Here,the x is an arrays variable.
2. Creating memory locations
There is a need to allocate memory new is a special operator that allocates memory.
var_name =new type[size];
Here, type specifies the type of data being allocated
size specifies the number of elements in the array
var_name is the array variable that is linked to the array.
Ex, x=new int[5];
It is also possible to combine two steps – declaration and creation into one step as follow.
int x[]=new int[5];
3. Initialization of array
To put the values into array is known as initialization.it can be done as:
var_name[subscript]=value;
Ex.
x[0]=11;
x[1]=12;
x[2]=13;
x[3]=14;
x[4]=15;
In above example it initialize the value of array element
Second Method
It is also possibe to initialize an array elements when they are created.
type var_name[]={list of values};
Here, the array initializer is a list of values separated by commas and surrounded by curly braces. there is no need to specify size of an array. The compiler allocates enough space for all elements specified in the list.
Ex, int mdays[ ]={30,31,30,31,30,31};
Multidimensional Array
Multidimensional arrays are actually array of arrays.
To declare a multidimensional array variable specify each additional index using another set of square bracket.
int no[][]=new int [4][5];
Here ,the no is two dimensional array with 4*5=20 elements
Array Length
In java , all arrays store the allocated size in a variable named length.
ex, int len= x.length;
Here, the length returns size of an array. It is useful when the array size is not known.
variabe size array
Multidimensional arrays are actually arrays of arrays, the length of each array is under your control. For example, the following program creates a two dimensional array in which the sizes of the second dimension are unequal.
class VariableSizeArray
{
public static void main(String args[])
{
int data[][] = new int[4][];
data [0] = new int[1];
data [1] = new int[2];
data [2] = new int[3];
data [3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
{
for(j=0; j<i+1; j++)
{
data [i][j] = k;
k++;
}
}
for(i=0; i<4; i++)
{
for(j=0; j<i+1; j++)
{
{
System.out.print(data [i][j] + " ");
}
}
System.out.println();
}
LANGUAGE
}
}
Posted by : Ruchita Pandya
Posted by : Ruchita Pandya
No comments:
Post a Comment