Lecture4
Lecture4
Applications Programming
LECTURE 4
Lists
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Lists
Learning Objectives
At the end of the lecture, you should be able to:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
What is a List ?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
1
List Structure
A List can be:
• one way – last cell has a null pointer
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
List Manipulation
Elements can be added and deleted
Add
Delete
List Features
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
2
A Simple List
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Review of Casting
In IPRG001, you used casting on primitive data types
3
Casting Object references
We can cast to
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
List Iterator
A list iterator (itr or litr) retrieves an element from the list
4
LinkedList Iterator Methods
add(Object O) inserts element into the list
set(Object o) replaces the last element returned with the specified element
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Quick Quiz
With a partner, discuss and answer these questions:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
ListIterator<Rectangle> li = rectangles.listIterator();
Rectangle rectangle1 = li.next(); //1st element in list
Rectangle rectangle2 = li.next(); //2nd element in list
Rectangle rectangle3 = li.next(); //3rd element in list
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
5
Iterating through a List
We can use a while loop to iterate through elements in the list
ListIterator<Rectangle> li = rectangles.listIterator();
while (li.hasNext())
{
Rectangle current = li.next();
System.out.println("Area is:" + current.area());
System.out.println("Perimeter is:" + current.perimeter());
}
Results:
Area is:6.0 – this is the first object
Perimeter is:10.0
Area is:12.0 – this is the second object
Perimeter is:14.0
Area is:20.0 – this is the third object
Perimeter is:18.0
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
for(initialisation;exit condition;increment)
for (element:collection)
• Colon separation
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
6
For-each Object Array Example
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
LinkedList<Customer> customers =
new LinkedList<Customer>();
customers.add(new Customer(" Peter "));
customers.add(new Customer(" Bill "));
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Loop Exits
• continue – jump to
Loop the end of the loop
• break – exit the loop
Exits • return – exit the loop
and the method
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
7
Enum
Is an enumerated data type, that is, you can declare a list of
constants that the runtime will treat as numbers.
Example:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Complex Enum
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
8
Complex Enum
Here we have an attribute called name, each field in the enum
has a value for name that is set when the constructor is called
by the runtime.
– ie HEARTS:name has a value of “Hearts”.
The public method getName() can be called to retrieve the
attribute value for each field.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Collections
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Key Concepts
Summary
A list can be one way or two
way
LinkedList implements a two
way list
A ListIterator retrieves an
object from a list
Enhanced for loop
for(element : collection)
An enum can be simple or
complex
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F