1 / 14

COMP206-08S General Programming 2

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() {

nardo
Télécharger la présentation

COMP206-08S General Programming 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP206-08SGeneral Programming 2

  2. Lectures • Today • Collections (Chapter 6 of Weiss) Department of Computer Science

  3. 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

  4. 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

  5. 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

  6. 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

  7. Searching public class binarySearch { // Finds a value in a sorted array private int[] a; public binarySearch(int[] anArray) { a = anArray; } Department of Computer Science

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

More Related