1 / 20

ArrayLists

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)

Download Presentation

ArrayLists

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. ArrayLists The lazy man’s array

  2. What’s the matter here? int[] list = new int[10]; list[0] = 5; list[2] = “hey”; list[3] = 15; list[4] = 23;

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

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

  5. 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 rosesYou smell like beautiful roses.You smell like POO!

  6. list like roses beautiful 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)+ ” ”);

  7. list 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 You roses beautiful

  8. list 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 You smell roses beautiful

  9. list 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 You smell roses beautiful

  10. list 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 You POO! smell roses beautiful

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

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

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

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

  15. Take 1: ILLEGAL:3 is NOT an object! ArrayListlist=newArrayList();list.add(3);System.out.println(list.get(0)+5); LEGAL:Wrap the int first ILLEGAL:Can’t add an Object to an int Take 2: ArrayListlist=newArrayList();Integermyint=newInteger(3);list.add(myint);Objectmyobj=list.get(0);System.out.println(((Integer)myobj).intValue()+5); LEGAL:Cast the Object as an Integer.THEN get it’s value. Using Wrapper Classes

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

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

  18. 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 Objectclass. • The Objectclass 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.

  19. Compile-Time Error! GOOD GOOD GOOD RUN-Time Error! ArrayList Amnesia: Example ArrayListlist=newArrayList(); list.add(newCD("Ima Artist","Ima Sings",15.50,3)); list.add(newCircle(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();

  20. 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 vs. ArrayList Array ArrayList

More Related