Title: CS445 Recitation 7:25pm8:15pm
1CS445 Recitation725pm-815pm
2The Helpdesk Hours located in the 5712 lab
3Homework 1
- Go over solution of homework 1
- (new Date()).getTime() number of ms since
January 1, 1970, 000000 GMT - Running Time
- Linear search 106 (around 220) comparisons
- Binary search log 220 20 comparisons
4Generic Types
- If the data fields of a class can be objects of
any class type, you can use a generic type when
declaring them - puclic class MyClassltTgt
- The identifier T can be any identifier
- MyClassltStringgt item new MyClassltStringgt()
- MyClassltIntegergt item new MyClassltIntegergt()
- Define more than one generic type with a class
definition - puclic class MyClassltT, S, Rgt
5Generic Types within an Interface
- public interface PairableltSgt
-
- public void setPair(S firstItem, S lastItem)
-
- A class that implements this interface
- public class OrderedPairltTgt implements
PairableltTgt - public class Circle implements ComparableltCirclegt,
Measurable
6Generic Types
Object o Box b (Box)o//casting public
interface ComparableltTgt public int
compareTo(T) //casting is NOT needed in method
body public interface Comparable public
int compareTo(Object o) //casting is needed in
method body
7ADT List
- Data
- A collection of objects in a specific order and
having the same data type - The number of objects in the collection
- Operations
- add(newEntry) at the end
- add(newPosition, newEntry) anywhere
- remove(givenPosition)
- clear() -- remove all
- replace(givenPosition, newEntry)
- getEntry(givenPosition)--look at
- contains(anEntry)
- getLength() -- count
- isEmpty() -- empty
- isFull() -- full
- display() -- display all the entries
8Implementing ADT List
- Using fixed-size Array
- add(newEntry) at the end
- add(newPosition, newEntry) anywhere
- remove(givenPosition)
- clear() -- remove all
- replace(givenPosition, newEntry)
- getEntry(givenPosition)--look at
- contains(anEntry)
- getLength() -- count
- isEmpty() -- empty
- isFull() -- full
9Implementing ADT List
- Using Array Expansion
- When an array is full, move its contents to a
larger arraythe dynamic expansion of an array. - isFull() always return false
int myArray new int100 //myArray is full,
expand it. int oldArray myArray myArray
new int2 oldArray.length for (int index0
index lt oldArray.length index)
myArrayindex oldArrayindex
Original array
Larger array
10ArrayList A Java Class Library class that use
dynamic array expansion
Â
Â
Each Expansion increase the room by 50.
11Â
12Â
13Why ArrayList ?
- Support generic type.
- Provide sorting and searching support for any
ArrayList of objects that implement Comparable
interface.
14Java Classes Implementing Comparable
15Examples
- http//www.cs.pitt.edu/aronis/cs445/misc/
DemonstrateArrayList.java - //////////////////////////////////////////////////
//////////////////// - /// Contents Demonstrate some capabilities of a
generic ArrayList - /// Author John Aronis
- //////////////////////////////////////////////////
//////////////////// - /// Java's ArrayList is a flexible, fast, and
extremely useful data structure. It comes in two
forms generic and vanilla. The generic form is - /// illustrated below. A few of the most useful
methods are illustrated see a good manual for
many more. - import java.util.ArrayList
- import java.util.Collections
- public class DemonstrateArrayList
- public static void main(String args)
- /// Create an ArrayList of Strings and fill
it - ArrayListltStringgt A new ArrayListltStringgt()
- A.add("d") A.add("b") A.add("e")
A.add("a") - A.add("h") A.add("f") A.add("c")
A.add("g") - System.out.println(A)
- /// Zip through it quickly
- for (int i0 iltA.size() i)
System.out.print( A.get(i) " " )
Output d, b, e, a, h, f, c, g d b e a h f c
g a, b, c, d, e, f, g, h 2 -9 a, hello, foo,
c, e, f, g, h
16Examples
- ArrayListltIntegergt B new ArrayListltIntegergt()
- B.add(6) B.add(2) B.add(4) B.add(9)
- B.add(1) B.add(8) B.add(9)
- System.out.println(B)
- /// Zip through it quickly
- for (int i 0 i lt B.size() i)
System.out.print(B.get(i) " ") - System.out.println()
- /// String implements Comparable, so sort it
with Java's Quicksort - Collections.sort(B)
- System.out.println(B)
- /// Search it with Java's binary search
- System.out.println(Collections.binarySearch(B,
6)) - System.out.println(Collections.binarySearch(B,
100)) - /// Demonstrate a few other important methods
- B.add(2, 33)
Output 6, 2, 4, 9, 1, 8, 9 6 2 4 9 1 8 9
1, 2, 4, 6, 8, 9, 9 3 -8 1, 111, 33, 4, 8, 9,
9
17Examples
- public class CBox implements ComparableltCBoxgt
- public static int numberOfBoxes 0
- private float side 0.0F
- public CBox(float s)
- side s
- numberOfBoxes
-
- public float getSide() return side
- public void setSide(float s) side s
- public CBox add(CBox b)
-
- CBox result new CBox(0.0F)
- result.setSide( this.side b.side )
- return result
-
- public boolean equals(CBox b)
- return (this.side b.side)
18Examples
- ArrayListltCBoxgt C new ArrayListltCBoxgt()
- C.add(new CBox(6)) C.add(new CBox(2)) C.add(new
CBox(4)) C.add(new CBox(9)) - C.add(new CBox(1)) C.add(new CBox(8)) C.add(new
CBox(9)) - System.out.println(C)
- /// Zip through it quickly
- for (int i 0 i lt C.size() i)
System.out.print(C.get(i) " ") - System.out.println()
- /// String implements Comparable, so sort it with
Java's Quicksort - Collections.sort(C)
- System.out.println(C)
- /// Search it with Java's binary search
- System.out.println(Collections.binarySearch(C,
new CBox(6))) - System.out.println(Collections.binarySearch(C,
new CBox(100))) - /// Demonstrate a few other important methods
- C.add(2, new CBox(222))
Output 6.0, 2.0, 4.0, 9.0, 1.0,
8.0, 9.0 6.0 2.0 4.0 9.0 1.0 8.0
9.0 1.0, 2.0, 4.0, 6.0, 8.0,
9.0, 9.0 3 -8 1.0, 111.0, 222.0,
4.0, 6.0, 8.0, 9.0, 9.0
Question When invoking the boolean
remove(Object o) method that is inherited from
List class, if C.remove(new CBox(8)) can remove
item from the right array list ?
19- /////
- ///// Contents Demonstrate how to extract
number from string. - ///// Author John Aronis
- ///// Date January 2007
- /////
- public class TestStringForNumber
- public static void main(String args)
- String s
- s "foo"
- System.out.println("\nString\""s"\" ---")
- if ( isNumber(s) ) System.out.println(
getNumber(s) ) - else System.out.println( "Can't fool me, that
isn't a number!" ) - s "1.2345"
- System.out.println("\nString\"" s "\"
---") - if (isNumber(s)) System.out.println(getNumber(s))
- else System.out.println( "Can't fool me, that
isn't a number!" ) - System.out.println()
Output String"foo" --- Can't fool me, that
isn't a number! String"1.2345" --- 1.2345
20ArrayList
- http//java.sun.com/j2se/1.4.2/docs/api/java/util/
ArrayList.html - http//java.sun.com/j2se/1.4.2/docs/api/java/util/
Collections.html