07ArrayLists
07ArrayLists
Programming Techniques
ArrayLists
Agenda & Reading
Topics:
Introduction to Collections and Class ArrayList
ArrayLists
ArrayList with Generics
Wrapper Classes
ArrayLists Vs Arrays
Reading
Java how to program Late objects version (D & D)
Chapter 6
The Java Tutorial
ArrayLists
http://www.tutorialspoint.com/java/java_arraylist_class.htm
2 Lecture07
1.Introduction
Introduction to Collections and Class ArrayList
Collections provide efficient methods that organize, store
and retrieve your data without requiring knowledge of how
the data is being stored.
ArrayLists:
Everything stored must be an Object (non-primitive data types)
Objects can be different types
provides methods for adding and removing
keeps track of the list capacity (the length of the allocated array)
and list size (the number of elements currently in the list)
Example: capacity
import java.util.ArrayList; size
...
ArrayList list = new ArrayList(); "Cat" "Hat" "Bat"
... ...
3 Old Lecture07
version
2.ArrayLists
Advantages & Disadvantages
Advantages:
Size is dynamic, rather than fixed.
Disadvantages:
Less efficient than arrays
Lacks familiar [ ] syntax (Java limitation, supported by C+
+)
Base type must be an object (not primitive) type
4 Lecture07
2.ArrayLists
Methods
5 Lecture07
2.ArrayLists
Methods to add elements
Only objects can be added to an ArrayList.
public boolean add(Object x)
list.add( "One" ); One Two Three
list.add( "Two" );
list.add( "Three" );
Adds an object to the end of the list, adjusts the size of the list; returns
true
Objects can be added to a specific position in an ArralyList:
public void "One"
list.add( add(int
); index, Object o)
One Fred Two Thre
list.add( "Two" ); e
list.add( "Three" );
list.add( 1, "Fred" );
7 Lecture07
2.ArrayLists
Size & Capacity
public boolean isEmpty ( )
True if empty; false otherwise.
System.out.println(list.isEmpty()); false
4
public void clear ( )
Removes all elements; size() becomes 0
list.clear();
8 Lecture07
2.ArrayLists
Methods to remove elements
Object remove(int index) One Fred Two Thre
e
where 0<=index<size() (or exception) Fred Two Thre
e
Removes element at index; shifts to the left remaining
elements at index+1 … size()-1.
list.remove( 0 ); String s = (String) list.remove( 0 );
System.out.println(s); One
System.out.println(list.set(0, "Hello"));
One
Hello Fred Two
System.out.println(list.set(4, "Good"));
Exception in thread "main"
public Object get ( int index ) java.lang.IndexOutOfBoundsException:
Hello
System.out.println(list.get(0));
Hello
Using
for( int a For
i=0; loop
i<list.size(); i++ ) { Fred
System.out.println(list.get(i).toString()); Two
}
11 Lecture07
2.ArrayLists
Equality
public boolean equals ( Object other )
True only when both are of the same size, and both have
the same elements with the same order.
list.add("Hello");
list.add("Two"); false
list.add("Fred");
myList2.add("Two");
myList2.add("Hello");
myList2.add("Fred");
System.out.println(list.equals(myList2));
12 Lecture07
2.ArrayLists
Different Types Old
version
13 Lecture07
Exercise 1
What is the output of the following code fragment?
ArrayList list = new ArrayList();
Point pt1 = new Point(3, 4);
list.add( pt1 );
Point pt2 = (Point) list.get( 0 );
pt2.x = 23;
if ( pt2 == pt1 ) {
System.out.println( "Same object" );
} else {
System.out.println( "Different object" );
}
System.out.println();
14 Lecture07
3.Generic ArrayLists
Starting with Java 5, ArrayList<T> and other collection classes
hold objects of a specified data type.
The T (by convention) is a placeholder—when declaring a new
ArrayList, replace it with the type of elements that you want the
ArrayList to hold.
Classes with this kind of placeholder that can be used with any type
are called generic classes.
The elements’ data type is shown in angle brackets and
becomes part ofwords
ArrayList<String> the =ArrayList type. For example:
new ArrayList<String>();
ArrayList<Integer> nums = new ArrayList<Integer>();
Elements are
Example: all in the same Standard for-
type. loop
for (String str : words)
System.out.println(str); for( int i=0; i<words.size(); i++ )
17 Lecture07
4.Wrapper Classes
Value => Object: Wrapper Object Creation
Wrapper.valueOf() takes a value (or string) and
returns an object of that class:
Integer i1 = Integer.valueOf(42);
Integer i2 = Integer.valueOf("42");
18 Lecture07
4.Wrapper Classes
Object => Value & String => value
Each wrapper class Type has a method typeValue to
obtain the object’s value: intValue(), doubleValue(),
charValue() etc
System.out.println(i1.intValue()); 42
System.out.println(c1.charValue()); b
System.out.println(b1.booleanValue()); true
19 Lecture07
4.Wrapper Classes
Auto boxing
Since Java 5, conversion from int to Integer and from
double to Double is, in most cases, automatic
(autoboxing/autounboxing)
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(12);
numbers.add(5); autoboxing
int sum = 0;
20 Lecture07
Exercise 2
What is the output of the following code fragment?
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(3);
x.add(5);
x.add(8);
x.add(9);
System.out.println(x);
System.out.println(y);
21 Lecture07
5.ArrayLists Vs Arrays
Capacity:
Array: Once you set the array size, you cannot change it easily.
int actualSize = . . .;
Point[] points1 = new Point[actualSize];
If the internal array is full, the array list automatically creates a bigger array
andArrayList<Point>
copies all the objects
points2from theArrayList<Point>(100);
= new smaller to the bigger array.
capacity = 100
ArrayList:
returns the actual number of elements in the ArrayList
23 Lecture07
5.ArrayLists Vs Arrays
Accessing Elements
To access or change the element:
Array: points1[i] = new Point(1,2);
Use [] syntax to access or change the element of an array
The raw ArrayList is also a bit dangerous. Its add and set methods accept objects of any
type and you run into grief only when you retrieve the object of another type and try to
cast it.
24 Lecture07
5.ArrayLists Vs Arrays
Converting between Array and ArrayList
Array to ArrayList java.awt.Point[x=1,y=2]
Use the asList() method in the Arrays class java.awt.Point[x=3,y=4]
Point[] points1 = {new Point(1,2), new Point(3,4)};
ArrayList<Point> points2 = new
ArrayList<Point>(Arrays.asList(points1));
for (Point p: points2)
System.out.println(p);
}
return uniqueList;
}
26 Lecture06