arraylists the lazy mans array. whats the matter here? int[] list = new int[10]; list[0] = 5;...
Post on 30-Mar-2015
219 Views
Preview:
TRANSCRIPT
ArrayLists
The lazy man’s array
What’s the matter here?
int[] list = new int[10];
list[0] = 5;
list[2] = “hey”;
list[3] = 15;
list[4] = 23;
What is it? ArrayList is a Java class that already exists (you
don’t 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
ArrayList 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
Using ArrayListsArrayList list = new ArrayList();list.add(“like”);list.add(“beautiful”);list.add(“roses”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.add(0, “You”);list.add(1, “smell”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.remove(3);list.set(3, “POO!”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);
like beautiful rosesYou smell like beautiful roses.You smell like POO!
0 1 2 3 4 5 6
list
like
beautiful
roses
ArrayList list = new ArrayList();list.add(“like”);list.add(“beautiful”);list.add(“roses”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.add(0, “You”);list.add(1, “smell”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.remove(3);list.set(3, “POO!”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);
ArrayList list = new ArrayList();list.add(“like”);list.add(“beautiful”);list.add(“roses”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.add(0, “You”);list.add(1, “smell”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.remove(3);list.set(3, “POO!”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);
0 1 2 3 4 5 6
list
like
beautiful
roses
You
ArrayList list = new ArrayList();list.add(“like”);list.add(“beautiful”);list.add(“roses”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.add(0, “You”);list.add(1, “smell”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.remove(3);list.set(3, “POO!”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);
0 1 2 3 4 5 6
list
like
beautiful
roses
You
smell
ArrayList list = new ArrayList();list.add(“like”);list.add(“beautiful”);list.add(“roses”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.add(0, “You”);list.add(1, “smell”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.remove(3);list.set(3, “POO!”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);
0 1 2 3 4 5 6
list
like
beautiful
roses
You
smell
ArrayList list = new ArrayList();list.add(“like”);list.add(“beautiful”);list.add(“roses”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.add(0, “You”);list.add(1, “smell”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);System.out.println();list.remove(3);list.set(3, “POO!”);for(int j=0; j<list.size(); j++)
System.out.print(list.get(j)+ ” ”);
0 1 2 3 4 5 6
list
like
beautiful
roses
You
smell
PO
O!
Dynamic 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
Using ArrayLists:You 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.
Objects 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
Wrapper Classes: they’re 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( ));
Using Wrapper Classes
ArrayList list = new ArrayList();list.add(3);System.out.println(list.get(0)+ 5);
Take 1: ILLEGAL:3 is NOT an object!
ILLEGAL:Can’t add an Object to an intArrayList list = new ArrayList();
Integer myint = new Integer(3);list.add(myint);Object myobj = list.get(0);System.out.println( ((Integer)myobj).intValue()+5);
Take 2:
LEGAL:Wrap the int first
LEGAL:Cast the Object as an Integer.THEN get it’s value.
Using Wrapper Classes:You Try Declare and instantiate an ArrayList Fill it with 5 of your favorite Integers Write a loop to calculate the SUM Output the sum
Store 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 Student[10];list[0] = new Student(“Sakib”);list[1] = new Fish( ); //ERROR ERROR
ArrayList list = new ArrayList( );list.add( new Student(“Sakib”) );list.add( new Fish( ) ); //NO PROBLEM
ArrayList 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.
ArrayList 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).getArtist();((CD)list.get(0)).getArtist();((Circle)list.get(1)).getRadius();((Shape)list.get(1)).area();((Shape)list.get(0)).area();
Compile-Time Error!
GOOD
GOODGOOD
RUN-Time Error!
Array vs. 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
Array ArrayList
top related