ArrayLists 26-Jul-16

advertisement
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
We will discuss autoboxing in some other lecture
2
Creating a ArrayList


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);


(10 is the initial size you want)
This is sort of like an array, where you would say
String[] ary = new String[10];
instead of
ArrayList<String> ary = new ArrayList<String>(10);
3
Adding elements to a ArrayList

boolean add(Type obj)



Appends the object obj to the end of this ArrayList
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, Type 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

The obj must be of the correct type, or you get a compile-time (syntax)
error
4
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
5
Getting values out

Type get(int index)


Returns the component at position index
Using get:

ArrayList<String> myList = new ArrayList<String>();
myList.add("Some string");
String s = myList.get(0);
6
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
7
Getting information

boolean isEmpty()


int size()


Returns true if this ArrayList has no elements
Returns the number of elements currently in this ArrayList
Type[ ] toArray(Type[ ])


Returns an array containing all the elements of this ArrayList
in the correct order
The parameter is usually a zero-length array of the correct
type

Example: String[] names = nameList.toArray(new String[0]);
8
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
9
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
10
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
11
Download