Presentation is loading. Please wait.

Presentation is loading. Please wait.

ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.

Similar presentations


Presentation on theme: "ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight."— Presentation transcript:

1 ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. TM

2 12½ - 2 Objectives: l Review interfaces l Learn about Comparable interface l Learn about the java.util.List interface and java.util.ArrayList class

3 12½ - 3 Interfaces l An interface specifies a list of one or more methods, giving only their signatures, but no code l A class implements an interface if it supplies code for all methods of that interface l If a class C implements an interface I, objects of C acquire a secondary data type, I l A class can implement several interfaces but it can extend only one class

4 12½ - 4 Interfaces: example public interface Sellable { double getPrice (); void setPrice (double price); } public class GroceryItem implements Sellable { private double myPrice; public double getPrice () { return myPrice; } public void setPrice (double price) { myPrice = price; } } Assumed public implements

5 12½ - 5 Interfaces (cont’d) l Why do we need them? –Situation one: public method calculateTax (Sellable obj, double rate) { return obj.getPrice() * rate; }... GrorceryItem apple = new GroceryItem("Apple", 0.30); double tax = calculateTax(apple, 0.05);... More generic type of parameter  more reusable code (works for any “Sellable” object)

6 12½ - 6 Interfaces (cont’d) –Situation two: private Sellable[ ] items = new Sellable[3]; items[0] = new GroceryItem(...); items[1] = new Pizza(...); items[2] = new JewelryItem(...); Different types of objects are mixed together in the same array or list

7 12½ - 7 Comparable Interface public interface Comparable { /** * Returns a positive integer if this is * "greater than" other, a negative integer if * this is "less than" other, and 0 if this is * "equal" to other */ int compareTo(Object other); } Kind of like this “minus” other

8 12½ - 8 Comparable (cont’d) «interface» java.lang.Comparable java.lang.Stringjava.lang.Integerjava.lang.Double compareTo is based on lexicographical order compareTo is based on numerical values

9 12½ - 9 Comparable example public class UsedCar implements Sellable, Comparable { private double myPrice; public int compareTo(Object other) { double diff = getPrice() - ((UsedCar) other).getPrice(); return (int)(100 * diff); // diff in cents } Forgetting a cast or parentheses results in a syntax error

10 12½ - 10 java.util.List Interface l The List library interface describes a list of objects in abstract terms l In a list, objects are arranged in sequence obj 0, obj 1,..., obj n - 1 l In Java, a list holds references to objects l A list can contain duplicate objects (both obj1.equals(obj2) and obj1 == obj2)

11 12½ - 11 List Methods (a Subset) int size(); boolean isEmpty (); boolean add (Object obj); void add (int i, Object obj); Object set(int i, Object obj); Object get(int i); Object remove(int i); boolean contains(Object obj); int indexOf(Object obj); returns true inserts obj as the i-th value; i must be from 0 to size() i must be from 0 to size() - 1 use equals to compare objects

12 12½ - 12 java.util.ArrayList l Implements List using an array l Keeps track of the list capacity (the length of the allocated array) and list size (the number of elements currently in the list) "Cat""Hat""Bat" capacity size

13 12½ - 13 ArrayList (cont’d) l Automatically increases (doubles) the capacity when the list runs out of space; allocates a bigger array and copies all the values into it l get(i) and set(i, obj) are efficient because an array provides random access to its elements l Throws IndexOutOfBoundsException when i < 0 or i  size()

14 12½ - 14 ArrayList (cont’d) l ArrayList holds objects (of any type) l If you need to put ints or doubles into a list, use a standard Java array or convert them into Integer or Double objects l You have to remember what types of objects your put into the list and may need to cast a retrieved object back into its type

15 12½ - 15 ArrayList (cont’d) l From Java API Docs:

16 12½ - 16 ArrayList list = new ArrayList (); list.add (new Integer(1)); list.add (new Integer(2)); list.add (new Integer(3)); int sum = 0; for (int i = 0; i < list.size(); i++) sum += ((Integer) list.get(i)). intValue(); ArrayList (cont’d) l Example 1 Need a cast to Integer in order to call intValue

17 12½ - 17 ArrayList words = new ArrayList (4); words.add ("One"); words.add ("Fish"); words.add ("Two"); words.add ("Fish"); int i = 0; while (i < words.size() ) { if ( ”Fish".equals (words.get(i)) ) words.remove(i); else i++; } ArrayList (cont’d) l Example 2 Shifts all the values after the i-th to the left and decrements the size

18 12½ - 18 Lab: Index Maker

19 12½ - 19 Index Maker (cont’d) IndexMaker IndexEntry DocumentIndex > java.util.List java.util.ArrayList implementsextends has

20 12½ - 20 Review: l How is an interface different from a class? l Can a class implement several interfaces? l Can an interface have more than one method? l If C is a class, when is the following statement valid? Comparable x = new C(); l What are the methods of Comparable?

21 12½ - 21 Review (cont’d): l Name the List methods that can add a value to the list. l Name the List methods that can tell you whether a given value is in the list. l In an ArrayList, should the indices be less than the size or less than the capacity? l What happens when the size of a List equals the capacity and you try to add a value?


Download ppt "ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight."

Similar presentations


Ads by Google