Download presentation
Presentation is loading. Please wait.
Published byMarlee Shott Modified over 10 years ago
1
ArrayLists The lazy mans array
2
Whats 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 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
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 roses You smell like beautiful roses. You smell like POO!
6
0123456 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)+ );
7
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)+ ); 0123456 list like beautiful rosesYou
8
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)+ ); 0123456 list like beautiful rosesYou smell
9
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)+ ); 0123456 list like beautiful rosesYou smell
10
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)+ ); 0123456 list like beautiful rosesYou smellPOO!
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: 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( ));
15
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.
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 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.
19
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!
20
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.