OOP U7 Array LIst Interface 2024s1
OOP U7 Array LIst Interface 2024s1
• Arrays
• Interface
2
Arrays
Index Number 0 1 2 3 4 5 6 7 8 9 10 11
2.3 8.9 2.3 5.1 8.2 1.1 0.1 9.9 2.1 2.2 8.9 2.2
4
Example of Accessing Array with for Statement
• The following examples declare and initialize arrays at the same time
int[] num = new int[] { 1, 3, 5 };
int[] number = { 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" };
number.length 4
samplingData.length 9
monthName.length 12
6
Array of Objects
7
Creating an Object Array - 1
person
State
of
Memory
After A is executed
8
Creating an Object Array - 2
person
person
0 1 2 3 4 16 17 18 19
State
of
Memory
After B is executed
9
Creating an Object Array - 3
person
person
0 1 2 3 4 16 17 18 19
State
of Person
Memory
After C is executed
10
Person Array Processing – Sample 1
12
Removing Object from Array – 1
person person
0 1 2 3 0 1 2 3
A B C D A C D
person person
0 1 2 3 0 1 2 3
A B C D A D C
15
Passing Arrays to Methods - 1
A public int
minOne = searchMinimum(float[] number))
searchMinimum(arrayOne); {
//...
}
At A before searchMinimum
arrayOne
A. Local variable
number does not
exist before the
method execution
State of
Memory
16
Passing Arrays to Methods - 2
public int B
minOne = searchMinimum(float[] number))
searchMinimum(arrayOne); {
//...
}
17
Passing Arrays to Methods - 3
public int
minOne = searchMinimum(float[] number))
searchMinimum(arrayOne); { C
//...
}
State of
Memory
18
Passing Arrays to Methods - 4
public int
minOne = searchMinimum(float[] number))
searchMinimum(arrayOne); {
D //...
}
At D after searchMinimum
number
D. The parameter is
arrayOne
arrayOne number erased, but the
argument still points
to the same object
State of
Memory
19
Two-Dimensional Arrays
• Declaration
<data type> [][] <variable> //variation 1
<data type> <variable>[][] //variation 2
• Creation
<variable> = new <data type>[<# of row>][<# of col>]
20
Declaring and Creating a 2-D Array
• Example: a2DTable
0 1 2 3 4
0
double[][] a2DTable; 1
a2DTable = new double[4][5]; 2
3
a2DTable[2][3] = 32.1; 21
Sample 2-D Array Processing
22
Multidimensional Arrays
// An Example of a 3D array
double[][][] sExMarks = new double[CLASSMAX][STUDENTMAX][EXMAX];
double[][][] sAvgMarks = new double[CLASSMAX][STUDENTMAX];
// ...
for (int i = 0; i < sExMarks.length; i++) { // loop for CLASS
for (int j = 0; j < sExMarks[i].length; j++) { // for STUDENT
double total = 0;
for (int k = 0; k < sExMarks[i][j].length; k++) //for Exercise
total += sExMarks[i][j][k];
sAvgMarks[i][j] = total / k; // find average mark of student
}
}
23
Collections and Lists
• When we add an element to an array that does not have any unused
position, we will have array overflow
• Solution 1 (Bad): Declare a larger array to avoid overflow, but it will end
up underutilizing the space
• Solution 2: Using more flexible types, such as Collection and List
(including ArrayList and LinkedList)
24
Collections and Lists
25
Class ArrayList
(In the package java.util)
• ArrayList is a “resizable-array” implementation of the List interface
– An array that automatically reallocates its capacity
26
Class LinkedList
(In the package java.util)
• LinkedList is the implementation of the List interface, based on
doubly-linked structure. Each element is contained in a linked node.
Doubly-linked-list.svg
: GraduateStudent
: UndergraduateStudent
: UndergraduateStudent
: GraduateStudent
31
Mixed Objects in an Array
• To compute the course grade using the course array, we execute
32
Type Checking: getClass() Method
int undergradCount = 0;
for (int i = 0; i < course.length; i++) {
if ( course[i].getClass() == UndergraduateStudent.class ){
undergradCount++;
}
}
33
Type Comparison Operator: instanceof
34
Using instanceof Operator
int undergradCount = 0;
for (int i = 0; i < course.length; i++) {
if (course[i] instanceof UndergraduateStudent ) {
undergradCount++;
}
}
35
Casting Objects
36
Abstract Class and Abstract Method
37
Abstract Class and Abstract Method
41
Java Interface Example
// declare an interface
public interface WorkHard {
int MIN_WORKING_HR = 12; // implicitly a final (constant)
public void spendTime(int sT);// implicitly public abstract method
public void concentrate();
}
• A Java class can inherit/extend from only one class but it can
implement more than one interface
43
Abstract Class vs. Interface
44
Implement Multiple Interfaces
public interface WorkHard { // declare an interface
int MIN_WORKING_HR = 12; // implicitly a final (constant)
public void spendTime(int sT); // implicitly abstract method
public void concentrate();
}
Student <<interface>>
WorkHard
GoodStudent <<interface>>
FollowRules
• You can define a class that does not implement all of the interface methods,
provided that the class is declared to be abstract, e.g.
public abstract class ABitHardWorker implements WorkHard {
public void spendTime(int sT){ //... implements method
}
}
47
Using Interface in GUI
• Using Java interface is important when producing applications with
GUI (Graphical User Interface)
* Ref: https://docs.oracle.com/en/java/javase/22/docs/api/java.desktop/java/awt/event/ActionListener.html
* Details of GUI will be discussed in later unit
48
Syntaxes for Class Declaration (RECAP)
49
Syntaxes for Class Declaration (RECAP)
E.g.: Below declares a class SubC, which is the subclass of class SuperC (its direct
superclass) and also implements two interfaces (InA, InB)
public class SubC extends SuperC implements InA, InB { //...
• Part of this slide set is referenced, extracted, and/or modified from the
followings:
– Deitel, P. and Deitel H. (2015) “Java How To Program”, 10ed, Pearson.
– Liang, Y.D. (2015) “Introduction to Java Programming”, Comprehensive
Version, 10ed, Prentice Hall.
– Wu, C.T. (2010) “An Introduction to Object-Oriented Programming with
Java”, 5ed, McGraw Hill.
– Oracle Corporation, “Java Language and Virtual Machine Specifications”
https://docs.oracle.com/javase/specs/
51