Download presentation
Presentation is loading. Please wait.
Published bySharon Dalton Modified over 9 years ago
1
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2
2
Department of Computer Science2 Lectures - Today - Collections (Chapter 6 of Weiss)
3
Department of Computer Science3 Some basics public class circle { private double radius; public circle(double r) { radius = r; } public double getRadius() { return radius; } public double area() { return Math.PI*radius*radius; }
4
Department of Computer Science4 Adding the ability to compare two circles public class circle implements Comparable { private double radius; public int compareTo(circle other) { if (radius < other.radius) return -1; if (radius == other.radius) return 0; return 1; } // Methods as per previous slide… }
5
Department of Computer Science5 Collections (sorting) import java.util.ArrayList; import java.util.Collections; public class listTest { public static void main(String[] args) { ArrayList circles = new ArrayList (); circles.add(new circle(8)); // add lots more
6
Department of Computer Science6 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()); }
7
Department of Computer Science7 Searching public class binarySearch { // Finds a value in a sorted array private int[] a; public binarySearch(int[] anArray) { a = anArray; }
8
Department of Computer Science8 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
9
Department of Computer Science9 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)
10
Department of Computer Science10 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 or LinkedList
11
Department of Computer Science11 Useful Methods void addFirst(E obj) // E is the thing LinkedList void addLast(E obj) E.getFirst() E.getLast() E.removeFirst() E.removeLast()
12
Department of Computer Science12 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 n = new LinkedList (); ListIterator iter = n.listIterator();
13
Department of Computer Science13 import java.util.LinkedList; import java.util.ListIterator; public class ListTester { public static void main(String[] args) { LinkedList n = new LinkedList (); 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); }
14
Department of Computer Science14 Note on the “for each” loop for (type variable : collection) Used with any collection class that implements Iterable interface public interface Iterable { Iterator iterator(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.