150 likes | 318 Views
ArrayList. Contents. General Properties Wrapper Classes Basic ArrayList Methods Adding to ArrayList V at a Position inx , where 0 inx < V.size( ) An Example – An ArrayList of Integers Adding to a Full ArrayList (Resizing) Retrieving Objects from an ArrayList. ArrayList .
E N D
ArrayList Contents • General Properties • Wrapper Classes • Basic ArrayList Methods • Adding to ArrayList V at a Position inx, where 0 inx < V.size( ) • An Example – An ArrayList of Integers • Adding to a Full ArrayList (Resizing) • Retrieving Objects from an ArrayList
ArrayList A “better” array In several respects an ArrayList may be considered a better array. It enhances the array in the following ways. It is generic – it holds elements of class Object It dynamically adjusts its capacity to meet the storage needs of the vector object It automatically adjusts its contents when an object is inserted or removed at an arbitrary location (index) However an ArrayList can only hold entities whose base class is type Object (descendants of class Object) Primitive types must be “wrapped” before they are inserted into an ArrayList. Objects removed from an ArrayList must be cast into their appropriate derived class
i x 24 24 Wrapper object is immutable ArrayList Wrapper Classes primitivetype Integer object Integer x = new Integer(i); int i = 24; System.out.println(x.IntValue( )); The value of the object that x refers to cannot be changed
Method Description ArrayList (Some) methods contained in class ArrayList ArrayList ( ) Constructs an empty ArrayList ArrayList (int cap) Constructs an Ar.List with initial capacity of cap boolean add(int i, Object obj) Inserts obj at position i of the ArrayList boolean add(Object obj) Add obj at the end of the ArrayList Objectremove(int i) Removes and returns obj at location i boolean remove(Object obj) Removes first occurrence of obj and returns true if found boolean contains(Object obj) returns true if obj is in the ArrayList int indexOf(Object obj) returns index of first occurrence of obj boolean set(int i, Object obj) Replaces element at location i with obj Object get (int i) Returns element at location i boolean isEmpty( ) Returns true if ArrayList is empty int size( ) Returns size of ArrayList
Alist 0 1 2 3 4 5 6 7 x 24 ArrayList The method add(int i, Object obj) Code: Integer x = new Integer(24); ArrayList Alist = new ArrayList(8); //ArrayList A has capacity of 8 //assume we have added five integers Alist.add(3, x); //add Integer object at location 3 0 0 0
ArrayList Import java.util.ArrayList (or java.util.*) A simple example – an ArrayList of integers //file intListExample.java import java.util.ArrayList; class intListExample { private ArrayList aList; public intListExample ( ) { aList = new ArrayList(10); for (int index = 0; index < aList.capacity( ); index++) { //use wrapper class to convert to object Integer inx = new Integer (index); aList.add(inx); //inserts at rear of aList } Constructor for example object initializes the vector with the integers from 0 to 9 Method continued on the next page
Main function simply calls constructor ArrayList //now show the contents of aList for (int index = 0; index < aList.size( ); index++) { //first retrieve the object stored in index Object obj = aList.get(index); //cast obj as the actual class type Integer inx = (Integer) obj; System.out.print(inx.toString( ) + " "); } } publicstaticvoid main(String[] args) { intListExample ivx = new intListExample( ); } }
ArrayList This same operation can be done using an iterator. ListIterator itr = aList.iterator( ); while (itr.hasNext( )) Object obj = itr.next( ); Integer myInt = (Integer)obj; System.out.println(myInt + “ ”); //implicitly uses Integer.toString( ) }
x 0 1 2 3 4 aList 0 1 2 3 4 5 6 21 ArrayList Adding an element to a full ArrayList Code: Integerx = new Integer(21); ArrayListaList = new ArrayList (5); //aList has capacity 5 //assume we have inserted 5 integers aList.add(x); //Add Integer x at back insert Integer x at newBuff[size] Declare locally – Object[] newBuff = new Object[7]; Set buffer = newBuff 0
aList 0 1 2 3 4 5 6 7 8 9 obj inx 7 ArrayList Retrieving Objects from an ArrayList Returning to the code for retrieving objects from an ArrayList: //now show the contents of aList for (int index = 0; index < aList.size( ); index++) { //first retrieve the object stored in index Object obj = aList.get(index); //cast obj as the actual class type Integer inx = (Integer) obj; System.out.print(inx.toString( ) + " "); } index = 0