Whats the matter here? int list = new int; list = 5; list = hey; list = 15; list = 23;
What 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
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 ArrayLists 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)+ ); like beautiful roses You smell like beautiful roses. You smell like POO!
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: 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( ));
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: Cant add an Object to an int ArrayList 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 its 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; list = new Student(Sakib); list = 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 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 ArrayArrayList