Array List in Java
Array List in Java
Jun-Oct 2013
CHAPTER TWO
LIST ADT (Pn Yusnita Sokman)
List
2
CSC248
Jun-Oct 2013
Cont
3
an element
l
ffrom a lilist
Insert a new element to a list
Delete an element from a list
Find how many elements are in a list
Find whether an element is in a list
Find whether a list is empty
List Implementation
4
Array
Use
List
a linked structure
Linked
List
CSC248
Jun-Oct 2013
List Implementation
5
List Implementation
6
Figure:
a0
a1
a2
a3
a4
an
a[0]
a[1]
a[2]
a[3]
a[4]
a[n]
head
tail
new list
Need
CSC248
Jun-Oct 2013
List Implementation
7
Empty
list
Need
Traverse
Can
list
List Implementation
8
and
dd
deletion
l i operation
i must maintain
i i the
h
characteristics after completed the process
To insert new element
Need
To
delete element
Need
CSC248
Jun-Oct 2013
List Implementation
9
15
12
17
15
21
24
35
40
44
17
19
21
24
35
40
44
44
15
17
21
24
35
40
12
15
17
21
24
40
44
10
CSC248
Jun-Oct 2013
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
11
Properties
Empty
list
size
is 0
head and tail reference null(a special value indicating they
dont reference a list position)
Nonempty
List
List
list
12
CSC248
Jun-Oct 2013
Properties
All
elements
l
in
i the
h list
li must b
be iin adjacent
dj
positions.
ii
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.
13
Operations/methods
E get
t (i
(int
t index);
i d )
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
14
CSC248
Jun-Oct 2013
Operations/methods
E set
t (i
(int
t index,
i d
E element);
l
t)
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
15
Operations/methods
int
i
t indexOf
i d Of (Object
(Obj t element);
l
t)
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
16
CSC248
Jun-Oct 2013
Operations/methods
Returns: nothing
17
Operations/methods
E remove (i
(int
t index);
i d )
Pre-condition: 0 <= index < size
Responsibilities: remove element at position index
Post-condition:
Returns:
CSC248
Jun-Oct 2013
public
19
Insertion
public
20
10
CSC248
Jun-Oct 2013
The
an
ArrayList
Suzi,
S
i
1234
Ali,
Ali
6543
Khai,
Kh
i
9876
21
Methods Specifications
22
22
11
CSC248
Jun-Oct 2013
Methods Specifications
23
[0]
[1]
[2]
[0]
[1]
[2]
apples
oranges
apples
fruits add (
fruits.add
(oranges);
oranges );
fruits.add (durian);
[0]
[1]
[2]
apples
oranges
durian
23
Methods Specifications
24
add
dd (i
(int
t index,
i d
E element);
l
t)
("apples");
("oranges");
("durian");
("apples");
(2,"cherries");
[0]
[1]
[2]
[3]
[4]
apples
oranges
cherries
durian
apples
24
12
CSC248
Jun-Oct 2013
Methods Specifications
25
("apples");
("oranges");
("durian");
("
("apples");
l ")
Then;
System.out.println (fruits.size());
//will output 4
25
Methods Specifications
26
26
13
CSC248
Jun-Oct 2013
Methods Specifications
27
Example:
fruits.add ("apples");
fruits.add ("oranges");
fruits.add ("durian");
fruits.add ("apples");
System.out.println (fruits.set(2, "bananas"));
27
Methods Specifications
28
Example:
fruits.add ("apples");
fruits.add ("oranges");
fruits.add ("durian");
fruits.add ("apples");
System.out.println (fruits.indexOf("durian"));
// will output 2
System.out.println (fruits.indexOf("kiwi"));
// will output 1
28
14
CSC248
Jun-Oct 2013
Methods Specifications
29
Example:
fruits.add ("apples");
fruits.add ("oranges");
fruits.add ("durian");
fruits.add ("apples");
System out println (fruits.remove(2));
System.out.println
(fruits remove(2));
29
EXERCISES
15
CSC248
Jun-Oct 2013
Exercise 1
31
12
78
90
31
Solution 1
32
16
CSC248
Jun-Oct 2013
Solution 2
33
aList.add("12");
aList.add("78");
Li t dd("78")
aList.add("1");
aList.add("7");
aList.add("90");
Solution 2
34
int data;
f (i t i = 0;
for(int
0 i<5;
i 5 i++)
i )
{
data=Integer.parseInt(JOptionPane.s
howInputDialog("Input number: "));
aList.add(data);
}
17
CSC248
Jun-Oct 2013
Solution 3
aList.set(3, "100");
35
Solution 4
for(int i=0;i<aList.size();i++)
{
Object n =aList.get(i);
int num=Integer.parseInt(n.toString());
if(num%3==0)
{
sList.add(num);
aList.remove(i);
i--;
}
}
36
18
CSC248
Jun-Oct 2013
Solution 5
System.out.println("\n// Data in aList");
for(int i=0;i<aList.size();i++)
{
System.out.println(aList.get(i));
}
37
Solution 6
System.out.println("The size of the
aList is " + aList.size());
aList size());
System.out.println("The size of the
sList is " + sList2.size());
38
19
CSC248
Jun-Oct 2013
Solution 7
int sum = 0;
for(int
(
i=0;i<sList.size();i++)
()
)
{
Object n =sList.get(i);
int num=Integer.parseInt(n.toString());
sum = sum + num;
}
System.out.println("Sum
y
p
(
of numbers in
sList: " + sum);
39
Exercise 2
40
20
CSC248
Jun-Oct 2013
Exercise 3
41
Exercise 2
42
int orderID;
String custName;
String prodName;
int prodQuantity;
double unitPrice;
Invoice(int oid, String cn, String pn, int pq, double up) {}
int getOrderID() {}
String getCustName() {
{}
}
String getProdName() {}
int getProdQuantity(){}
double getUnitPrice(){}
String toString() {}
21
CSC248
Jun-Oct 2013
Continue
43
22