Arrays
Arrays
BY:
M.I. Fathima Nihla
Lecturer (Prob.)
Department of Management & IT
Department of MIT / SEUSL
Java language provides some facilities to manipulate data in the form of
groups or sets.
One way of achieving this is by using a data structure known as an Array.
An Array is a collection of related data items of the same type that share a
collective name.
Each data item of an array is called an element and elements are accessed
individually by using an index or subscripted variable.
An array can be a single dimensional or multi-dimensional and single
dimensional array needs one subscript whereas multi-dimensional arrays
need more than one subscript.
An array subscript starts with 0.
To create an array, you must declare a variable of the correct type and instantiate an
array object that the variable refers to.
To declare an array variable, you code a set of empty brackets after the type or the
variable name.
To instantiate an array, you use the new keyword and specify the length, or size, of the
array in brackets following the array type.
You can specify the array length by coding a literal value or by using a constant or
variable of type int.
When you instantiate an array of primitive types, numeric types are set to zeros and
boolean types to false.
When you create an array of objects, they are set to nulls.
Department of MIT / SEUSL 10
CREATE AN ARRAY OF INT DATA TYPE THAT HOLDS THE LIST OF 10 EVEN
NUMBERS.
HINTS:
ASK THE USER TO ENTER THE ARRAY SIZE.
BASED ON THE SIZE OF THE ARRAY, CREATE A DOUBLE TYPE ARRAY.
ASSIGN VALUES FOR THE ARRAY USING A FOR LOOP; USE THE LOOP’S COUNTER VALUE
FOR THE ARRAY.
DISPLAY THE TOTAL AND AVERAGE OF THE VALUES ON THE SCREEN.
THANK YOU!