1 / 10

ArrayList

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 .

ulf
Download Presentation

ArrayList

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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( ); } }

  8. 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( ) }

  9. 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

  10. 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

More Related