Thursday 3 November 2011

Arrays in Java| Core Java tutorials

Arrays in Java:
An array is a group of similar data type items that share common name.
Arrays occupy contiguous memory locations.
Array size is fixed and cannot be altered dynamically.
 A particular value is indicated by writing a number called index number or subscript in square brackets after the array name.
For example we can define an array name employee to represent a set of salaries of  a group of employees. For example
Employee [3]
Represents the fourth employee starting from zero.
Complete set of values is referred to as array, the individual values are called elements.
Arrays can be of any primitive datatypes. It is also possible to create array of objects.
One dimensional arrays:
A list of items can be given one variable name using only one subscript and such a group of variables is called one dimensional array.
Ex:int count[];
double  [] average;
Multidimensional array
It is actually an array of arrays. To declare a multidimensional array variable, each additional index should be specified using another set of square brackets.
Int marks [][];
Declaration of Arrays:
Ex:int count[];
double  [] average;
Creating one dimensional array:
Arrays must be declared and created in the computer memory before they are used. Creation of Array involves
a)      Declare the array.
b)      Create memory location
c)       Assigning the values in memory location

Declaration of Arrays:

Arrays in java may be declared in two forms:
 a)      datatype array- name[];
b)      datatype [] arrayname;

No comments:

Post a Comment