Transcript ArrayLists

ArrayLists
26-Jul-16
ArrayLists and arrays


A ArrayList is like an array of Objects
Differences between arrays and ArrayLists:


Arrays have special convenience syntax; ArrayLists don’t
An array is a fixed size, but a ArrayList expands as you add
things to it


This means you don’t need to know the size beforehand
Arrays can hold primitives or objects, but ArrayLists can
only hold objects

However, autoboxing can make it appear that an ArrayList can hold
primitives
2
Generics

In Java versions 1.4 and earlier, an ArrayList just
held Objects, which could be of any (non-primitive)
type


For compatibility reasons you can still do this, but you will
get a lot of warning messages
In Java 5 you should specify the class of objects that the
ArrayList will hold

This is done with the new generics syntax
3
Creating a ArrayList the old way



The syntax for creating ArrayLists has changed
between Java 1.4 and Java 5
For compatibility reasons, the old way still works, but
will give you warning messages
Here are the (old) constructors:


import java.util.ArrayList;
ArrayList vec1 = new ArrayList();


Constructs an ArrayList with an initial capacity of 10
ArrayList vec2 = new ArrayList(initialCapacity);
4
Using an ArrayList the old way


boolean add(Object obj)

Appends the object obj to the end of this ArrayList

Example: myArrayList.add("A string is an object");
Object get(int index)



Returns the component at position index
Note that this is returned as an Object; to use it as a String,
you must cast it
Example: String s = (String)myArrayList.get(5);
5
Creating a ArrayList the new way


Specify, in angle brackets after the name, the type of
object that the class will hold
Examples:



ArrayList<String> vec1 = new ArrayList<String>();
ArrayList<String> vec2 = new ArrayList<String>(10);
To get the old behavior, but without the warning
messages, use the <?> wildcard

Example: ArrayList<?> vec1 = new ArrayList<?>();
6
Adding elements to a ArrayList

boolean add(Object obj)



Appends the object obj to the end of this ArrayList
With generics, the obj must be of the correct type, or you get a
compile-time (syntax) error
Always returns true


This is for consistency with other, similar classes
void add(int index, Object element)

Inserts the element at position index in this ArrayList

The index must be greater than or equal to zero and less than or equal
to the number of elements in the ArrayList

With generics, the obj must be of the correct type, or you get a
compile-time (syntax) error
7
Removing elements

boolean remove(Object obj)




void remove(int index)


Removes the first occurrence of obj from this ArrayList
Returns true if an element was removed
Uses equals to test if it has found the correct element
Removes the element at position index from this ArrayList
void clear()

Removes all elements
8
Accessing with and without generics

Object get(int index)


Using get the old way:


ArrayList myList = new ArrayList();
myList.add("Some string");
String s = (String)myList.get(0);
Using get the new way:


Returns the component at position index
ArrayList<String> myList = new ArrayList<String>();
myList.add("Some string");
String s = myList.get(0);
Notice that casting is no longer necessary when we retrieve an
element from a “genericized” ArrayList
9
Searching a ArrayList

boolean contains(Object element)



int indexOf(Object element)




Tests if element is a component of this ArrayList
Uses equals to test if it has found the correct element
Returns the index of the first occurrence of element in this ArrayList
Uses equals to test if it has found the correct element
Returns -1 if element was not found in this ArrayList
int lastIndexOf(Object element)



Returns the index of the last occurrence of element in this ArrayList
Uses equals to test if it has found the correct element
Returns -1 if element was not found in this ArrayList
10
Getting information

boolean isEmpty()


int size()


Returns true if this ArrayList has no elements
Returns the number of elements currently in this ArrayList
Object[ ] toArray()

Returns an array containing all the elements of this ArrayList
in the correct order
11
More about equals

There are many different notions of equality


Example: two sets are equal if they contain the same elements;
order of elements is irrelevant
Java defines public boolean equals(Object) in the
Object class, but


equals is defined to be the same as ==
It’s often a good idea to override equals for your own objects


If you do this, note that the argument should be a general Object
The String class (and some others) override equals
12
Conclusion




A ArrayList is like an array of Objects
The advantage of a ArrayList is that you don’t need to know
beforehand how big to make it
The disadvantage of a ArrayList is that you can’t use the
special syntax for arrays
You should never use an array that you hope is “big
enough”—use a ArrayList instead
13
The End
“Where a calculator on the ENIAC is
equipped with 18 000 vacuum tubes and
weighs 30 tons, computers of the future may
have only 1 000 vacuum tubes and perhaps
weigh 1½ tons.”
--Popular Mechanics, March 1949
14