Ch11 - Array List
Ch11 - Array List
Polymorphism
(ArrayList)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
1
Objectives
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
2
● What you do if you want to save 100 student’s
records?
Student[] array= new Students[100];
You can create an array to store objects. But the
array’s size is fixed once the array is created.
● What if the number of students increases??
copy the array to bigger one
● What if you don’t know the size??
ArrayList class;
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
The ArrayList Class
Java provides the ArrayList class that can be used to store an
unlimited number of objects.
Creating and adding contents:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
4
Differences and Similarities between
Arrays and ArrayList
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
5
The ArrayList Class
Removing contents and checking status:
+contains(o: Object): boolean Returns true if this list contains the element o.
+isEmpty(): boolean
Returns true if this list contains no elements.
+size(): int
Returns the number of elements in this list.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
7
Array Vs. ArrayList
● String [] s = new String [10];
– s[0]=“One”; s[1]=“two”; ….
This is a specific kind of array with particular
size.
● ArrayList s = new ArrayList ();
● ArrayList <E> s = new ArrayList<E> ();
– s.add(“One”); s.add(“two”); ….
Use generic ArrayList.
<E> is called generic type. You can replace it
with concrete object type
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
9
Generic Type
You can specify a concrete type to replace E when
creating an ArrayList.
For example,
This ArrayList object can be used to store strings.
TestArrayList Run
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
10
ArrayList s= new ArrayList();
s.add(“one”);
ERROR → required casting
String n= s.get(0);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
12
System.out.println("List size? " + cityList.size());
System.out.println("Is Miami in the list? " + cityList.contains("Miami"));
System.out.println("The location of Denver in the list? " +
cityList.indexOf("Denver"));
System.out.println("Is the list empty? " + cityList.isEmpty());
// Print false
// Insert a new city at index 2
cityList.add(2, "Xian");
// contains [London, Denver, Xian, Paris, Miami, Seoul, Tokyo]
// set a city at index 3
cityList.set(3, "Jeddah");
// contains [London, Denver, Xian, Jeddah, Miami, Seoul, Tokyo]
// Remove a city from the list
cityList.remove("Miami");
// contains [London, Denver, Xian, Paris, Seoul, Tokyo]
// Remove a city at index 1
cityList.remove(1);
// contains [London, Xian, Paris, Seoul, Tokyo]
// Display the contents in the list
System.out.println(cityList.toString());
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
13
// Display the contents in the list in reverse order
for (int i = cityList.size() - 1; i >= 0; i--)
System.out.print(cityList.get(i) + " ");
System.out.println();
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
14
The MyStack Classes
MyStack
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
15
import java.util.ArrayList;
public class MyStack
{
private ArrayList<Object> list = new ArrayList<>();
public boolean isEmpty()
{ return list.isEmpty(); }
public int getSize()
{ return list.size(); }
public Object peek()
{ return list.get(getSize() - 1); }
public Object pop()
{
Object o = list.get(getSize() - 1);
list.remove(getSize() - 1);
return o;
}
public void push(Object o)
{ list.add(o); }
@Override /** Override the toString in the Object class */
public String toString()
{ return "stack: " + list.toString(); }
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
16
Array Lists from/to
Arrays
We can convert primitive type to objects using Wrapper classes.
Can we convert ArrayList to array and Vice Versa???
YES
Using Static method “asList” in the Arrays Class to convert
array to ArrayList.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
17
Array Lists from/to
Arrays
We can convert primitive type to objects using Wrapper classes.
Can we convert ArrayList to array and Vice Versa???
YES
Using method “toArray” in the ArrayList to covert ArrayList
to array .
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
18
If you want to sort , find max/ min, or shuffle an array, what you
do ?
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
19
sort,max,min and shuffle in an
Array List
Integer[] array={5,8,2,9,4};
Collections.sort(list); [2, 4, 5, 8, 9]
9
System.out.println(list); 2
[4, 5, 9, 2, 8]
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));
Collections.shuffle(list);
System.out.println(list);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
20