0% found this document useful (0 votes)
7 views

Arrays

Uploaded by

Shibly Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arrays

Uploaded by

Shibly Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MIT11053 Fundamentals of Programming 1

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.

Department of MIT / SEUSL 2


Index (or Subscript) of the element in array C

Name of the array: C

Department of MIT / SEUSL 3


 Array objects occupy space in memory. Like other objects, arrays are created with
keyword new.
 To create an array object, you specify the type of the array elements and the
number of elements as part of an array-creation expression that uses keyword new.

 The declaration and initialization can also be done as shown below

Department of MIT / SEUSL 4


 To refer to the elements, we use an index that ranges from zero (the first element
in the array) to one less than the total number of elements.
 If you specify an index that is less than zero or greater than the upper bound of the
array, an ArrayIndexOutOfBoundsException will be thrown.

Department of MIT / SEUSL 5


Department of MIT / SEUSL 6
Department of MIT / SEUSL 7
 You can instantiate an array and provide initial values with an array initializer—a
comma-separated list of expressions (called an initializer list) enclosed in braces.
 The number of values determines the size of the array.

Department of MIT / SEUSL 8


Department of MIT / SEUSL 9
 An array can store more than one primitive type or object.

 An element is one of the items in an array.

 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.

 PRINT ALL THE ARRAY ELEMENTS.

Department of MIT / SEUSL 11


 MODIFY THE PROGRAM IN EXERCISE 01 TO PERFORM THE FOLLOWING:

 FIND THE SUM OF ALL NUMBERS AND PRINT THE TOTAL.

 FIND THE LARGEST NUMBER OUT OF ALL AND PRINT IT.

Department of MIT / SEUSL 12


 WRITE A JAVA PROGRAM TO INITIALIZE THE MARKS FOR A SUBJECT IN AN ARRAY.
CALCULATE AND PRINT THE TOTAL AND THE AVERAGE.

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.

Department of MIT / SEUSL 13


14

THANK YOU!

Department of MIT / SEUSL

You might also like