9 ArrayLists
9 ArrayLists
con (drawbacks)
ArrayIntList only works for ints (arrays can
be any type)
Need to learn how to use the class
Java Collections and ArrayLists
Java includes a large set of powerful classes
that provide functionality for storing and
accessing collections of objects
The most basic, ArrayList, can store any type
of Object.
https://docs.oracle.com/javase/8/docs/api/
Iterating through an array list
Suppose we want to look for a value in an ArrayList of
Strings.
for (int i = 0; i < list.size(); i++) {
if(value.equals(list.get(i)){
//do something
}
}
Alternative:
for (String s : list) {
if(value.equals(s)){
//do something
}
}
Note on generics in Java 7 and above
In version 7 of Java, rather than doing:
ArrayList<Type> name = new ArrayList<Type>();
You can save a few keystrokes:
ArrayList<Type> name = new ArrayList<>();
Modifying while looping
Consider the following flawed pseudocode for
removing elements that end with ‘s’ from a list:
removeEndS(list) {
for (int i = 0; i < list.size(); i++) {
get element i;
if it ends with an 's', remove it.
}
}
What does the algorithm do wrong?
index 0 1 2 3 4 5
value "she" "sells" "seashells" "by" "the" "seashore"
size 6
ArrayList of primitives?
The type you specify when creating an ArrayList
must be an object type; it cannot be a primitive type.
The following is illegal:
// illegal -- int cannot be a type parameter
ArrayList<int> list = new ArrayList<int>();
But we can still use ArrayList with primitive types by
using special classes called wrapper classes in their
place.
ArrayList<Integer> list = new ArrayList<Integer>();
Wrapper classes: Example
Every java primitive has a class dedicated
to it.
Example:
int x = 3;
Integer y = new Integer(5);
int z = x + y;
y = new Integer(“5”);
16
ArrayLists of wrapper type objects
Primitive Wrapper
Type Type
int Integer
double Double
char Character
boolean Boolean
float
A wrapper is an object Float
whose purpose is to hold a primitive value
and to provide more functionality.
Once you construct the list, use it with primitives as normal
(autoboxing):
ArrayList<Double> grades = new ArrayList<Double>();
grades.add(3.2);
grades.add(2.7);
ArrayLists of wrapper type objects
Autoboxing:
ArrayList<Double> grades = new ArrayList<Double>();
// Autoboxing: create Double from double 3.2
grades.add(3.2);
grades.add(2.7);
double sum = 0.0;
for (int i = 0; i < grades.size(); i++) {
//AutoUNboxing from Double to double
sum += grades.get(i);
}
...
Java Collections
ArrayList belongs to Java’s Collections
framework.
Other classes have a very similar interface, so it
will be easier to learn how to use those classes
once you’ve learned ArrayList
Looking ahead: Interfaces
A Java interface specifies which public methods
are available to a user
A class implements an interface if it provides all
the methods in the interface
Interfaces allow for common behavior amongst
classes. Example: the List interface is
implemented by several Collections classes
(LinkedList, ArrayList, Vector, Stack)
Linked Lists
Example:
int [] values = new int[5];
int x = 1;
values 5 7 10 6 3
x 1
Java References
When one reference variable is assigned to another,
the object is not copied; both variables refer to the
same object.
a1 index 0 1 2 3 4 5 6
value 7
4 5 2 12 14 14 9
a2
Self references
Consider the following class:
public class StrangeObject {
String name;
StrangeObject other;
}
Will this compile?
Linking self-referential nodes
public class IntegerNode {
int item;
IntegerNode next;
}
Each node object stores:
one piece of integer data
a reference to another node
Into this?
Into this?
list.getNext().setNext(new IntegerNode(30));
A more flexible version
public class Node {
private Object item; Node node = new Node (5);
private Node next; Java will convert 5 to an instance
public Node(Object item) {
this.item = item;
of class Integer
this.next = null;
}
public Node(Object item, Node next) {
this.item = item;
this.next = next;
}
public void setNext(Node nextNode) {
next = nextNode;
}
public Node getNext() {
return next;
}
public Object getItem() {
return item;
}
public void setItem(Object item){
this.item = item;
}
}
}
Printing a linked list
Suppose we have a chain of nodes:
int[] a = ...;
int i = 0;
while (i < a.length) {
System.out.println(a[i]);
i++;
}
Printing a linked list
Same thing with a for loop
Node head = ...;
int[] a = ...;
public LinkedList() {
head = null; head =
size = 0; size = 0
}
...
}
Implementing add
How do we add to a linked list at a given
index?
After:
size = 2
42 20
element 0 element 1
Removing the first node from a list
Before removing element at index 0:
item nex item nex item nex
head = t t t
size = 3
42 -3 20
element 0 element 1 element 2
After:
size = 2
-3 20
element 0 element 1
List with a single element
Before: After:
dat nex
head = a t head =
size = 1
20 size = 0
element 0
And
Write string s minus its first character backward
And
write the list minus its first node backward