140 likes | 259 Views
COMP206-08S General Programming 2. Lectures. Today Collections (Chapter 6 of Weiss). Some basics. public class circle { private double radius; public circle( double r) { radius = r; } public double getRadius() { return radius; } public double area() {
E N D
Lectures • Today • Collections (Chapter 6 of Weiss) Department of Computer Science
Some basics publicclass circle { privatedouble radius; public circle(double r) { radius = r; } publicdouble getRadius() { return radius; } publicdouble area() { return Math.PI*radius*radius; } } Department of Computer Science
Adding the ability to compare two circles publicclass circle implements Comparable<circle>{ privatedouble radius; publicint compareTo(circle other) { if (radius < other.radius) return -1; if (radius == other.radius) return 0; return 1; } // Methods as per previous slide… } Department of Computer Science
Collections (sorting) import java.util.ArrayList; import java.util.Collections; publicclass listTest { publicstaticvoid main(String[] args) { ArrayList<circle> circles = new ArrayList<circle> (); circles.add(new circle(8)); // add lots more Department of Computer Science
Continued double sum = 0; for (circle a : circles) { sum += a.area(); } System.out.println("Total area = "+sum); Collections.sort(circles); for (circle a : circles) { System.out.println(a.getRadius()); } } } Department of Computer Science
Searching public class binarySearch { // Finds a value in a sorted array private int[] a; public binarySearch(int[] anArray) { a = anArray; } Department of Computer Science
Continued public int search(int v) { int low = 0; int high = a.length - 1; while (low <= high) { int mid = (low + high) / 2; int diff = a[mid] - v; if (diff == 0) // a[mid] == v return mid; else if (diff < 0) // a[mid] < v low = mid + 1; else high = mid - 1; } return -1; } } // end class Department of Computer Science
Beyond arrays • Linked Lists • Idea is to maintain a list of nodes each having a reference to the next node • Adding and removing elements is efficient • Random access not possible only sequential (which is slow) Department of Computer Science
Consider a linked list of names • David, Harry, Richard, Tom • Insert Jane (needs to go between Harry and Richard) – note the operations needed • Java provides a LinkedList class in java.util • The class is generic so • LinkedList<String> or LinkedList<Circle> Department of Computer Science
Useful Methods void addFirst(E obj) // E is the thing LinkedList<E> void addLast(E obj) E.getFirst() E.getLast() E.removeFirst() E.removeLast() Department of Computer Science
What about the middle? • Java provides a ListIterator type • It encapsulates a position anywhere inside the list • Think of it as pointing between two nodes LinkedList<String> n = new LinkedList<String>(); ListIterator<String> iter = n.listIterator(); Department of Computer Science
import java.util.LinkedList; import java.util.ListIterator; publicclass ListTester { publicstaticvoid main(String[] args) { LinkedList<String> n = new LinkedList<String>(); n.addLast("Dave"); n.addLast("Harry"); n.addLast("Richard"); n.addLast("Tom"); ListIterator itr = n.listIterator(); // |DHRT itr.next(); // D|HRT itr.next(); // DH|RT itr.add("Jane"); // DHJ | RT itr.add("Neville"); // DHJN | RT itr.next(); // DHJNR | T // remove last traversed element itr.remove(); // DHJN | T for (String name : n) System.out.println(name); } } Department of Computer Science
Note on the “for each” loop • for (type variable : collection) • Used with any collection class that implements Iterable interface public interface Iterable<E> { Iterator<E> iterator(); } Department of Computer Science