0% found this document useful (0 votes)
58 views

Ch11 - Array List

The document discusses using an ArrayList to store objects instead of an array when the number of objects is unknown. It describes how to create and populate an ArrayList, add/remove elements, search elements, and compare ArrayLists to arrays. The ArrayList allows dynamic resizing, while arrays have a fixed size. The document provides examples of adding cities to an ArrayList, checking the size, searching for elements, inserting/updating/removing elements, and iterating in reverse order.

Uploaded by

moe pay
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Ch11 - Array List

The document discusses using an ArrayList to store objects instead of an array when the number of objects is unknown. It describes how to create and populate an ArrayList, add/remove elements, search elements, and compare ArrayLists to arrays. The ArrayList allows dynamic resizing, while arrays have a fixed size. The document provides examples of adding cities to an ArrayList, checking the size, searching for elements, inserting/updating/removing elements, and iterating in reverse order.

Uploaded by

moe pay
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 11 Inheritance and

Polymorphism

(ArrayList)

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
1
Objectives

● To store, retrieve, and manipulate objects in an ArrayList (§11.11).

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:

+ArrayList() Creates an empty list.


+add(o: E) : void Appends a new element o at the end of this list.
Adds a new element o at the specified index in this
+add(index: int, o: E) : void
list.
+set(index: int, o: E) : E Sets the element at the specified index. Return and
remove the current one

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
4
Differences and Similarities between
Arrays and ArrayList

Operation Array ArrayList


Creating an String[] a = new String[10] ArrayList<String> list = new
array/ArrayList ArrayList<>();
Adding a new element a[size-1]=”London”; list.add("London");

Inserting a new Needs shifting procedures list.add(index, "London");


element a[index]= "London";

Updating an element a[index] = "London"; list.set(index, "London");

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
5
The ArrayList Class
Removing contents and checking status:

Removes all the elements from this list.


+clear(): void
+remove(o: Object): boolean Removes the element o from this list.
+remove(index: int) : boolean Removes the element at the specified index.

+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.

Operation Array ArrayList


Removing an element Needs shifting list.remove(index);
procedures
Removing an element list.remove(Object);

Removing all elements a= null; list.clear();

Returning size a.length list.size();


Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
6
The ArrayList Class
Searching for contents :

Returns the element from this list at the specified index.


+get(index: int) : E
+indexOf(o: Object) : int Returns the index of the first matching element in this list.
+lastIndexOf(o: Object) : int Returns the index of the last matching element in this list.

Operation Array ArrayList


Accessing an element a[index] list.get(index);

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.

ArrayList<String> cities = new ArrayList<String>();


ArrayList<String> cities = new ArrayList<>();

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);

ArrayList s= new ArrayList();


s.add(“one”);
String n= (String) s.get(0); SOLVE CASTING

ArrayList < Sting> s= new ArrayList<String>();


s.add(“one”);
String n= s.get(0);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
11
public class TestArrayList
{
public static void main(String[] args)
{
// Create a list to store cities
ArrayList<String> cityList = new ArrayList<>();
// Add some cities in the list
cityList.add("London");
// cityList now contains [London]
cityList.add("Denver");
// cityList now contains [London, Denver]
cityList.add("Paris");
// cityList now contains [London, Denver, Paris]
cityList.add("Miami");
// cityList now contains [London, Denver, Paris, Miami]
cityList.add("Seoul");
// contains [London, Denver, Paris, Miami, Seoul]
cityList.add("Tokyo");
// contains [London, Denver, Paris, Miami, Seoul, Tokyo]

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();

// Create a list to store two circles


java.util.ArrayList<Circle> list = new java.util.ArrayList<>();

// Add two circles


list.add(new Circle (2));
list.add(new Circle(3));

// Display the area of the first circle in the list


System.out.println("The area of the circle? " + list.get(0).getArea());
}
}

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
14
The MyStack Classes
MyStack

● Implement each Method

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.

String[] array = {"red", "green", "blue"};


ArrayList<String> list = new ArrayList<>(Arrays.asList(array));

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 .

String[] array1 = new String[list.size()];


list.toArray(array1);

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 ?

Use an Algorithm that needs many statement and


looping.
Are there another and easier solutions????
Yes. Use the static methods “sort”, “max”, “min” and
“Shuffle” in the java.util.Collections class
But the objects in the ArrayList must be comparable
( Wrappers and Strings) objects to perform the tasks

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};

ArrayList<Integer> list= new ArrayList<>(Arrays.asList(array));

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

You might also like