2/1/2020 Course Teacher: S A M Matiur Rahman 1
SE 211 Object Oriented Concept
S A M Matiur Rahman,
Associate Professor & Associate Head
Department of Software Engineering
Permanent Campus
2/1/2020 SE-211 2
Course Teacher: S A M Matiur Rahman
SE 211 (Object Oriented Concepts)
Array in Java
2/1/2020 SE-211 3
Course Teacher: S A M Matiur Rahman
Array
Objectives:
Operate a
collection of data
values using an
array.
Use an array of
primitive data
types in writing a
program.
Manipulate array
in methods.
SE-211
2/1/2020 4
Course Teacher: S A M Matiur Rahman
Visual Representation of an Array
The following figure shows an empty array:
SE-211
2/1/2020 5
Course Teacher: S A M Matiur Rahman
Visual Representation of an Array
The following figure shows an array
contains elements that are of the same type:
SE-211
2/1/2020 6
Course Teacher: S A M Matiur Rahman
Visual Representation of an Array
The figure shown below is not supported in Java,
because one of the types is different from the
others.
SE-211
2/1/2020 7
Course Teacher: S A M Matiur Rahman
Visual Representation of an Array
When an array is populated with values, we
can visualize as in the following picture:
SE-211
2/1/2020 8
Course Teacher: S A M Matiur Rahman
Array
Array Basics
Array
Array Declaration
Indexed Expression of Array
Array Element
Testing Code Fragment.
The Array Class.
Duplicating an Array.
Testing the Methods in the Array Class.
Multidimensional Array.
Example Two Dimensional Array
SE-211
2/1/2020 9
Course Teacher: S A M Matiur Rahman
Array of Primitive Data Type
Array Declaration
<data type> [ ] <variable>; // option 1
<data type> <variable> [ ]; // option 2
Array Creation
<variable> = new <data type> [ <size> ];
Example:
Option 1 Option 2
int [ ]daysInWeek ; int daysInWeek [ ];
daysInWeek = new int [ 7 ]; daysInWeek = new int [ 7 ];
Array is like an object!
SE-211
2/1/2020 10
Course Teacher: S A M Matiur Rahman
Array Declaration
An array is a collection of data values of the same type. We
may declare an array consisting of double, but not an array
consisting of both int and double. The following declares an
array of double:
double [] rainfall;
The square bracket indicate the array declaration. The
bracket may be attached to a variable instead of the data
type. For example, the declaration
double rainfall [];
is equivalent to the previous declaration.
SE-211
2/1/2020 11
Course Teacher: S A M Matiur Rahman
Array Declaration
The following statement allocated the memory to store 12
double values and associates the identifier rainfall to it.
rainfall = new double [12]; // create an array of size 12
the new operator to allocate the memory for an array, but
an array is not an object.
We can also declare and allocate memory for an array in one
statement, as in
double [] rainfall = new double [12];
SE-211
2/1/2020 12
Course Teacher: S A M Matiur Rahman
Accessing Individual Element of an Array
• Individual element of an array accessed with the
indexed expression.
double[] rainfall = new double[12];
rainfall
0 1 2 3 4 5 6 7 8 9 10 11
The index of the first
position in an array is 0. The index expression
refers to the element
rainfall[3]
at position # 3.
SE-211
2/1/2020 13
Course Teacher: S A M Matiur Rahman
Index Expression of an Array
An individual value in an array is called array
element.
Zero – based indexing is used to indicate the
positions of an element in the array. They are
numbered 0, 1, 2, 3, ……….and size – 1, where size is
the size of the array.
For example, to refer to the third element of the
rainfall array, we use the indexed expression
rainfall[2];
The index for the first position in an array is zero. As
for String object, Java uses zero-based indexing for
an array.
SE-211
2/1/2020 14
Course Teacher: S A M Matiur Rahman
General Format of Array Declaration
The general format for declaration of array is:
DataType ArrayName[];
ArrayName = new DataType[ArraySize];
The above two statement can be written in single
statement:
DataType[] Arrayname = new DataType[ArraySize];
Other examples:
char Name[] = new char[20]; // char type array
float GPA[] = new float[5]; // float type array
SE-211
2/1/2020 15
Course Teacher: S A M Matiur Rahman
Array Initialization
Like other data type, it is possible to declare and
initialize an array at the same time.
int[] counter = { 2, 4, 6, 8 };
double[] samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2,
9.00, 3.123, 22.084, 18.08 };
String[] monthName = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };
counter.length 4
samplingData.length 9
monthName.length 12
SE-211
2/1/2020 16
Course Teacher: S A M Matiur Rahman
Program on Array Initialization
SE-211
2/1/2020 17
Course Teacher: S A M Matiur Rahman
Program on Array Initialization
SE-211
2/1/2020 18
Course Teacher: S A M Matiur Rahman
Variable-size declaration
In Java we do not limit to fixed-size array declaration.
The following code prompts the user for the size of an
array and declares an array of designated size.
Scanner scanner = new Scanner(System.in);
int size;
int[] number;
System.out.print("Size of an array:"));
size = scanner.nextInt( );
number = new int[size];
SE-211
2/1/2020 19
Course Teacher: S A M Matiur Rahman
Program on Array Initialization using Keyboard
SE-211
2/1/2020 20
Course Teacher: S A M Matiur Rahman
Program on Array Initialization using Keyboard
SE-211
2/1/2020 21
Course Teacher: S A M Matiur Rahman
Program on Array Initialization using Keyboard
SE-211
2/1/2020 22
Course Teacher: S A M Matiur Rahman
Array Initialization with Declaration
SE-211
2/1/2020 23
Course Teacher: S A M Matiur Rahman
Assigning Values to an Array Element
SE-211
2/1/2020 24
Course Teacher: S A M Matiur Rahman
Array Class
Array classes are available in package java.util.
Following are four basic static methods that
performs utility functions.
equals() – compare two arrays for equality.
fill() – to fill an array with a value.
sort() – to sort the array.
binarySearch() – to find an element in a sorted
array.
All of these methods are overloaded for all the
primitive types and Objects.
SE-211
2/1/2020 25
Course Teacher: S A M Matiur Rahman
Built-in Methods in the Array class
SE-211
2/1/2020 26
Course Teacher: S A M Matiur Rahman
Built-in Methods in the Array class
The program shows how to construct a String object with repeated
instance of single character. First we define a to be an array of 10
chars. Then we use the Arrays.fill() method to fill it with the
desired character, in this case an ‘A’. Then we use the appropriate
String constructor to produce a String object with the same
contents.
In the example, we create two int arrays, x and y, using the
Object.clone() method to duplicate x in y. Then it invokes the
Arrays.equals() method to check the equality. Then it returns true.
The Arrays.sort() method is used to sort x. It sorted all the
elements in the array.
SE-211
SE-211
2/1/2020 27
Course
CourseTeacher:
Teacher: S AS
MA M Matiur
Matiur Rahman Rahman
User Defined Methods in the Program
SE-211
2/1/2020 28
Course Teacher: S A M Matiur Rahman
Program on Testing Range
SE-211
2/1/2020 29
Course Teacher: S A M Matiur Rahman
Program on Multiplication Table
SE-211
2/1/2020 30
Course Teacher: S A M Matiur Rahman
Multidimensional Array
In Java, multidimensional arrays are actually arrays of
arrays. To declare a multidimensional array variable,
specify each additional index using another set of square
brackets.
For example, the following declares a two-dimensional
array variable called twoD.
int twoD[][] = new int[4][5];
This allocates a 4 by 5 array and assigns it to twoD.
Internally this matrix is implemented as an array of
arrays of int. Conceptually, this array will look like the
one shown in.
SE-211
2/1/2020 31
Course Teacher: S A M Matiur Rahman
Declaration and Creation of Multidimensional Array
Declaration:
<data type> [][] <variable> //variation 1
<data type> <variable>[][] //variation 2
Creation:
<variable> = new <data type> [ <size1> ][ <size2> ]
Example
payScaleTable
0 1 2 3 4
double[][] payScaleTable;
0
payScaleTable
1
= new double[4][5];
2
3
SE-211
2/1/2020 32
Course Teacher: S A M Matiur Rahman
Access of an Element in Two-dimensional Array
An element in a two-dimensional array is accessed
by its row and column index.
SE-211
2/1/2020 33
Course Teacher: S A M Matiur Rahman
Two-dimensional Array
1.4.1 Software efficiency is most strongly influenced by
the data structure and algorithm.
1.4.2 An efficient algorithm will remain efficient.
1.4.3 Java provides many data structures as Java
Collection Framework.
1.4.4 Java is most widely used programming languages in
both commercial and academic marketplace.
2/1/2020
SE-211 34
Course Teacher: S A M Matiur Rahman
2/1/2020
SE 211 35
Course Teacher: S A M Matiur Rahman