Title: ArrayLists
1ArrayLists
2Whats the matter here?
- int list new int10
- list0 5
- list2 hey
- list3 15
- list4 23
3What is it?
- ArrayList is a Java class that already exists
(you dont have to write it) - An ArrayList allows you to store Objects in
sequential order. - An ArrayList has functions that do important
array stuff like - Get the size
- Retrieve the element at an index
- Insert an element at a given index
- Remove the element at a given index
- Change the element at a given index
- Find a given element
4ArrayList functions
- int size() returns the number of elements
Note regular arrays have a member variable named
length. ArrayList blah.size( )
Regular array blah.length - boolean add(Object x) appends x to the end of
list returns true - Object get(int index) returns the element at the
specified location (without removing it!) - Object set(int index, Object x) replaces the
element at index with x and returns the element
formerly at index - void add(int index, Object x) inserts x at
position index, sliding elements to the right and
adjusting size - Object remove(int index) removes the element at
index, sliding elements to the left and adjusting
size.
Remember Indices start at 0
5Using ArrayLists
ArrayList list new ArrayList()list.add(like)
list.add(beautiful)list.add(roses)for(in
t j0 jltlist.size() j) System.out.print(list.
get(j) )System.out.println()list.add(0,
You)list.add(1, smell) for(int j0
jltlist.size() j) System.out.print(list.get(j)
) System.out.println()list.remove(3)list.
set(3, POO!)for(int j0 jltlist.size()
j) System.out.print(list.get(j) )
like beautiful rosesYou smell like beautiful
roses.You smell like POO!
6list
0 1 2 3 4 5 6
like
roses
beautiful
ArrayList list new ArrayList()list.add(like)
list.add(beautiful)list.add(roses)for(in
t j0 jltlist.size() j) System.out.print(list.
get(j) )System.out.println()list.add(0,
You)list.add(1, smell) for(int j0
jltlist.size() j) System.out.print(list.get(j)
) System.out.println()list.remove(3)list.
set(3, POO!)for(int j0 jltlist.size()
j) System.out.print(list.get(j) )
7list
0 1 2 3 4 5 6
ArrayList list new ArrayList()list.add(like)
list.add(beautiful)list.add(roses)for(in
t j0 jltlist.size() j) System.out.print(list.
get(j) )System.out.println()list.add(0,
You)list.add(1, smell) for(int j0
jltlist.size() j) System.out.print(list.get(j)
) System.out.println()list.remove(3)list.
set(3, POO!)for(int j0 jltlist.size()
j) System.out.print(list.get(j) )
like
You
roses
beautiful
8list
0 1 2 3 4 5 6
ArrayList list new ArrayList()list.add(like)
list.add(beautiful)list.add(roses)for(in
t j0 jltlist.size() j) System.out.print(list.
get(j) )System.out.println()list.add(0,
You)list.add(1, smell) for(int j0
jltlist.size() j) System.out.print(list.get(j)
) System.out.println()list.remove(3)list.
set(3, POO!)for(int j0 jltlist.size()
j) System.out.print(list.get(j) )
like
You
smell
roses
beautiful
9list
ArrayList list new ArrayList()list.add(like)
list.add(beautiful)list.add(roses)for(in
t j0 jltlist.size() j) System.out.print(list.
get(j) )System.out.println()list.add(0,
You)list.add(1, smell) for(int j0
jltlist.size() j) System.out.print(list.get(j)
) System.out.println()list.remove(3)list.
set(3, POO!)for(int j0 jltlist.size()
j) System.out.print(list.get(j) )
0 1 2 3 4 5 6
like
You
smell
roses
beautiful
10list
ArrayList list new ArrayList()list.add(like)
list.add(beautiful)list.add(roses)for(in
t j0 jltlist.size() j) System.out.print(list.
get(j) )System.out.println()list.add(0,
You)list.add(1, smell) for(int j0
jltlist.size() j) System.out.print(list.get(j)
) System.out.println()list.remove(3)list.
set(3, POO!)for(int j0 jltlist.size()
j) System.out.print(list.get(j) )
0 1 2 3 4 5 6
like
You
POO!
smell
roses
beautiful
11Dynamic Size
- No more logical / physical size dilemma!
- With ArrayLists, you DO NOT need to specify a
logical size at instantiation. - ArrayLists are always full, but never filled up.
- Physical size is the same as logical size(There
are no empty spaces at the end) - ArrayLists will make room for another element
12Using ArrayListsYou Try
- Declare and instantiate an ArrayList
- Insert 5 of your favorite words
- Print out the list (you need a loop)
- Remove the middle word
- Print out the list again.
13Objects Only!
- ArrayLists can ONLY store Objects. No primitive
data types allowed! - Nice for user defined classes
- Remember that ALL classes inherit from the Object
class. - Therefore any instance of any class that you
write is really an instance of Object, and will
fit into the ArrayList! - Not nice for primitive data types
- We have to trick the ArrayList into accepting
things like ints and doubles. - This is accomplished by using wrapper classes
Integer and Double
14Wrapper Classes theyre very simple
- Integer
- Integer(int value) constructor that simply
stores the number value into an object - int intValue( ) returns the stored number value
- Double
- Double(double value) constructor that simply
stores the number value into an object - double doubleValue( ) returns the stored number
value
Integer bob new Integer(15)Integer fred new
Integer(bob.intValue() 5)System.out.println(
fred.intValue( ))
15Using Wrapper Classes
16Using Wrapper ClassesYou Try
- Declare and instantiate an ArrayList
- Fill it with 5 of your favorite Integers
- Write a loop to calculate the SUM
- Output the sum
17Store Several Data Types
- Unlike in an array, all the elements of an
ArrayList DO NOT need to be of the same type. - You can create an array of Students, but it
cannot store Fish! - An ArrayList could store both Students and Fish,
since they are both Objects
Student list new Student10list0 new
Student(Sakib)list1 new Fish( ) //ERROR
ERROR
ArrayList list new ArrayList( )list.add( new
Student(Sakib) )list.add( new Fish( ) )
//NO PROBLEM
18ArrayList Amnesia
- Unfortunately, when you add any element to an
ArrayList, he forgets his data type. - When you retrieve the element, he only remembers
that he is an instance of the Object class. - The Object class has the following functions
- boolean equals(Object other)
- String toString( )
- Note The data type may override these
functions. - If you want to call any function besides these
two, you will need to cast the Object. Remind
him of his original data type.
19ArrayList Amnesia Example
ArrayList list new ArrayList() list.add(new
CD("Ima Artist", "Ima Sings", 15.50,
3)) list.add(new Circle(5)) list.get(0).getArti
st() ((CD)list.get(0)).getArtist() ((Circle)list
.get(1)).getRadius() ((Shape)list.get(1)).area()
((Shape)list.get(0)).area()
20Array vs. ArrayList
Array
ArrayList
- Fixed Physical Size
- Must specify at instantiation
- Limited storage capacity
- Only 1 Data Type
- All elements must be of the SAME type
- Primitive data types allowed
- Dynamic Size
- Never specify size
- The ArrayList never fills up.
- Store Several Types
- Can store any combination of objects
- No primitive data types allowed