Presentation is loading. Please wait.

Presentation is loading. Please wait.

(like an array on steroids)

Similar presentations


Presentation on theme: "(like an array on steroids)"— Presentation transcript:

1 (like an array on steroids)
The ArrayList Class (like an array on steroids)

2 Enjoy your colorful new AP Computer Science Reference Sheet…
Find the list of methods above this line: class java.util.ArrayList<E> implements java.util.List<E>

3 Creating Arrays and Array Lists
Create an array of ints: int[] iarray = new int[10]; Create an array of Fractions: Fraction[] farray = new Fraction[10]; Create an instance of the ArrayList class: ArrayList list = new ArrayList(); ArrayList <Fraction> list = new ArrayList <Fraction>();

4 Generic ArrayList Syntax
Create an ArrayList of Fractions: ArrayList <Fraction> list = new ArrayList <Fraction>( ); Create an ArrayList of class E: ArrayList <E> list = new ArrayList <E>( ); (where E is any known class: String, Fraction, Pet, etc. Create an ArrayList of Objects: ArrayList <Object> list = new ArrayList <Object>( ); OR ArrayList list = new ArrayList( );

5 A little overview What can you do with an array?
Most of ArrayList functionality is very similar to an array’s functionality Uses methods instead of [ ] More diverse in storage than arrays, yet they have a severe limitation: Sadly, an ArrayList can only store Objects So you WON’T see: ArrayList <int> list = new ArrayList <int>();

6 Array List method signatures
Note the importance of the parameter type and the return type in the following method declaration. What does each word mean? public Fraction add (Fraction f)

7 Array List method signatures
ArrayList <E> list = new ArrayList <E>( ); 1 2 3 4 5 6 7 8 9 3/4 1/2 1/9 2/5 3/7 9/2 69/2 13/17 So what do the following method declarations really mean? E get(int index) E set(int index, E x) boolean add(E x) void add(int index, E obj) (should be called insert)

8 Array List Methods int size() // Returns the number of items in the array list boolean add(E obj) //Adds an object to the end of the array list E get (int index) // Returns the object at the specified index of the array list. E set (int index, E x) void add(int index, E obj) // Inserts an object in a certain index of the array list. E remove(int index) // Removes and returns the object at the index void clear() // Removes all objects from the list int indexOf(E obj) // Returns the index of the first occurrence of the object int lastIndexOf(E obj) // Returns the index of the last occurrence of the object.

9 Array Lists and Arrays– Similarities and Differences
See the back of the notes sheet

10 Arrays vs ArrayLists Arrays ArrayLists
random-access, linear data structure fixed size once created dynamic size; grows automatically can contain objects and primitives can only contain objects must declare element type should declare element type <E> safe: run-time bounds checking Fraction[] myArray = new Fraction[15]; ArrayList<E> myList = new ArrayList<E>(); myArray[index] = new Fraction(1,2); myList.set(index, new Fraction(1,2)); myList.add(new Fraction(1,2)); Fraction f1 = myArray[index]; Fraction f1 = myList.get(index); for ( int k = 0; k < myArray.length; k++ )    System.out.println(myArray[k]); for ( int k = 0; k < myList.size(); k++ )    System.out.println(myList.get(k)); Methods in AP Subset:    boolean add(E obj)    void add(int index, E obj)    E get(int index)    E set(int index, E obj)    E remove(int index)    boolean remove(E obj)    int size()    Iterator iterator() – to be discussed later    Iterator listIterator() – to be discussed later Arrays vs ArrayLists

11 Array Lists – Simple code
String s1 = “Tina”; ArrayList <String> names = new ArrayList<String>(); names.add(s1); names.set(0, “Ryan”); System.out.println(names.get(1)); What will display to the screen? Answer: Index out of Bounds Exception Error

12 Array Lists – Simple code
String s1 = “Tina”; ArrayList <String> names = new ArrayList<String>(); names.add(s1); names.add(0, “Ryan”); System.out.println(names.get(1)); What will display to the screen? Answer: Tina

13 Something you will forget…
The ArrayList class is part of the java.util library. In order to use ArrayLists, this must be imported: import java.util.ArrayList; If you forget to import this library, your code will not build.

14 A limitation of ArrayLists
ArrayLists can ONLY contain objects. This is why wrapper classes are required. Now you’re probably asking yourself “Self, What the heck is a wrapper class?”

15 What the heck is a wrapper class?
The short answer: it is a way to convert primitive data types to objects so that they can be stored in data structures that only store objects. If you want to store an int i, you must make it an Integer like this: Integer XX = new Integer(i);

16 Sample Program #1 Answer: C
What is the output of the following code segment? ArrayList list = new ArrayList(); list.add(“A”); list.add(“B”); list.add(“C”); list.add(“D”); list.add(“E”); int k; for (k = 1; k <= 3; k++) { list.remove(1); } //Code continues to the right for (k = 1; k <= 3; k++) { list.add(1, new Integer(k)); } for (k = 0; k < list.size(); k++) System.out.print(list.get(k) + “ “); A C D E 1 2 3 1 2 3 B C D E A E A E 1 2 3 IndexOutOfBoundsException Answer: C

17 Sample Program #2 Which line causes the error and why? 1.
2. 3. 4. 5. Which line causes the error and why? ArrayList <String> list = new ArrayList<String>(); list.add(“A”); list.add(“B”); list.add(2, “D”); list.add(4, “F”); Answer: 5. list.add(4, “F”);

18 Sample Program #3 Which line causes the error and why? 1.
2. 3. 4. 5. Which line causes the error and why? ArrayList <String> list = new ArrayList <String>(); list.add(“A”); list.add(“B”); list.set(2, “D”); list.set(4, “F”); Answer: 4. list.set(2, “D”);

19 DANGER: IndexOutOfBounds
(Assume there is at least one element in list.) You can add from location 0 to location ?? You can set from location 0 to location ?? list.size() list.size() – 1

20 What is wrong with this line?
//Assume list is populated with lots of names. list.set(0) = “Bubba”; Answer: Are you crazy? Look at the signature!

21 Skip casting for now… ArrayList Assignment Hints: Don’t forget to:
import java.util.ArrayList; (Top line of file) Properly handle the user’s mistakes. Clearly SPECIFICALLY communicate the user’s mistakes back to him/her. Test all of the examples on the assignment sheet!!!!

22 To be covered later… Ignore the following slides for now.

23 Array Lists – Complications of casting (you loved it in Intro…)
double d; int x = d; Fix: int x = (int) d;

24 Array Lists – Complications of casting (you’ll really love it now…)
Consider this line: Fraction f1 = list.get(0); Alternatives: Fraction f1 = (Fraction)list.get(0); int x = ((Fraction)list.get(0)).getDenom();


Download ppt "(like an array on steroids)"

Similar presentations


Ads by Google