CHAPTER 2 - ARRAY LIST (New)
CHAPTER 2 - ARRAY LIST (New)
LIST ADT
List
2
2
Comparison between array and ArrayList
Array ArrayList
Fixed Length (static data structure) Variable length - size starts from zero, grows when you add
element and shrink when you remove element (dynamic data
structure)
Elements of specific data type Holds a collection of object references
If the data has a known number of elements or small fixed size when there will be a large variation in the amount of data that you
upper bound, or where efficiency in using primitive types is want to put into an array.
important
Can hold primitive data and object types it holds only object types and not primitive types (eg, int).
Characteristics : Characteristics :
• the size of the array must be given by the time the program is • The size of the array can be given at compile time or at runtime,
compiled so the application can begin execution and wait until it has a better
• The memory allocated for the array is permanently bound to idea of its space needed before allocating the array.
the array variable name • The memory that is allocated is not permanently bound to the
• The size of the array is fixed for the duration of the program array variable. It is perfectly legal to assign a different block of
so the array cannot shrink or grow to meet the user’s needs. memory to array variable.
• The size of the block that is allocated is fixed, but it is possible to
allocated a new block of memory that is smaller or larger than the
original block
List
4
4
Cont…
5
7
List Implementation
8
◻ Figure:
a0 a1 a2 a3 a4 an
a[0] a[1] a[2] a[3] a[4] a[n]
head tail
◻ To create new list, determine empty list and
traverse the list, it is quite easy:
� Create new list
■ Need to define an array variable type
■ Need variables to count the element in the list
8
List Implementation
9
� Empty list
■ Need to test the variables that count the number of
element in the list. If the value is zero – empty list
� Traverse list
■ Can be done using for statement
◻ For insertion and deletion operation it is quite
difficult depend on types of list
9
List Implementation
10
10
List Implementation
11
Insert 19 into list
12 15 17 21 24 35 40 44
12 15 17 19 21 24 35 40 44
12 15 17 21 24 35 40 44
12 15 17 21 24 40 44
11
The List Interface
12
12
The List ADT
13
◻ Description
� A list is a linear collection that supports indexing of its
elements.
� The list is mutable, unordered, has no fixed limit on size,
and allows duplicates.
� The list element type is unspecified
◻ Attribute
� size: the number of elements in the list, the range of
occupied positions is 0 to size –1
� head: the first element of the list
� tail: the last element of the list
13
The List ADT
14
◻ Properties
� Empty list
■ size is 0
■ head and tail reference null(a special value indicating they
don’t reference a list position)
� Nonempty list
■ List has one element
■ size is 1; head and tail refer to the same position
■ List has more than one element
■ size is the number of elements in the list; head refers to the
first element, tail refer to the last element, and these elements
are in different positions
14
The List ADT
15
◻ Properties
� All elements in the list must be in adjacent positions.
� The index of the first position is 0 and the index of the
last position is size –1; every indexed position refers to
a list element.
15
The List ADT
16
◻ Operations/methods
� E get (int index);
■ Pre-condition: 0 <= index < size
■ Responsibilities: returns the elements stored at the given
index position
■ Post-condition: the list is unchanged
■ Returns: element at position index
16
The List ADT
17
◻ Operations/methods
� E set (int index, E element);
■ Pre-condition: 0 <= index < size
■ Responsibilities: replace the element at position index with
new element (E)
■ Post-condition: the element previously at index is replaced
by new element (E); size, head and tail are unchanged
■ Returns: the element that had previously been at position
index
17
The List ADT
18
◻ Operations/methods
� int indexOf (Object element);
■ Pre-condition: none
■ Responsibilities: determine index of the first occurrence of
target in the list
■ Post-condition: list is unchanged
■ Returns: the index of the first occurrence of target in the
list if found, or –1 if not found
18
The List ADT
19
◻ Operations/methods
� void add (int index, E element);
■ Pre-condition: 0 <= index < size
■ Responsibilities: insert element into the list at the specified
location
■ Post-condition:
■ element is inserted in the list at position index
■ size is incremented by 1
■ head references the new element only if the list was previously
empty, otherwise head is unchanged
■ tail references the element in the first position if the list was
previously empty, otherwise tail ‘s position is advanced by 1
■ Returns: nothing
19
The List ADT
20
◻ Operations/methods
� E remove (int index);
■ Pre-condition: 0 <= index < size
■ Responsibilities: remove element at position index
■ Post-condition:
■ Element at position index is removed from the list
■ size is decremented by 1
■ head refers to null if the list is now empty, else refer to the
element in the first position
■ tail position is decremented by 1; tail is null if the list is empty
■ Returns: the element removed
20
The ArrayList Class
21
21
The ArrayList Class
22
22
The ArrayList Class
23
23
Methods Specifications
24
24
Methods Specifications
25
25
Methods Specifications
26
26
Methods Specifications
27
27
Methods Specifications
28
28
Methods Specifications
29
29
Methods Specifications
30
30
Methods Specifications
31
31
EXERCISES
Lets try this
33 Determine output for the following statement:
import java.util.ArrayList;
public class ArrayListProj {
public static void main(String[] args) {
E
String[] fruits = new String[4];
fruits[0] = "Mango"; x
fruits[1] = "Apple";
fruits[2] = "Strawberry"; e
fruits[3] = "Watermelon";
System.out.println(fruits);
r
ArrayList fruitList = new ArrayList();
fruitList.add("Mango"); c
fruitList.add("Apple");
fruitList.add("Strawberry");
fruitList.add("Watermelon");
i
fruitList.remove("Strawberry"); s
fruitList.clear();
System.out.println(fruitList.contains("Raspberry")); e
System.out.println(fruitList);
}
}
1
Lets
try
Determine output for the following statement:
import java.util.ArrayList;
34
this public class ArrayListProj {
public static void main(String[] args) {
String[] fruits = new String[4];
fruits[0] = "Mango";
E fruits[1] = "Apple";
fruits[2] = "Strawberry";
x
fruits[3] = "Watermelon";
System.out.println(fruits);
i
System.out.println(fruitList);
fruitList.remove("Strawberry");
s System.out.println(fruitList);
System.out.println(fruitList.contains("Raspberry"));
e System.out.println(fruitList.contains("Avocado"));
fruitList.clear();
System.out.println(fruitList);
}
}
2
Lets try this
35 Determine output for the following statement:
import java.util.ArrayList;
}
}
3
Exercise 4
36
37
Exercise 6
38
public Invoice(int oid, String cn, String pn, int pq, double up) {…}
public int getOrderID() {…}
public String getCustName() {…}
public String getProdName() {…}
public int getProdQuantity(){…}
public double getUnitPrice(){…}
public String toString() {…}
}
Continue…
40