CSE 214 – Computer Science I More on Linked Lists
Eclipse Video Tutorials Accessible online via schedule page Setting up Projects Debugging Learn to use the debugger ASAP I’ll repeat this all day if I have to
What’s an Iterator? Common approach to abstracting out iteration Essential to using data structures in Java & C++ especially c++ & especially lists Iterators are more important for lists than for arrays. Why?
Accessing elements in a list Philosophy of some list-managers: if you need to access a list element by index then you shouldn’t really be using a list Iterators are typically used instead: boolean hasNext() Returns true if the iteration has more elements. E next() Returns the next element in the iteration. void remove() Removes from the underlying collection the last element returned by the iterator (optional operation).
Using an Iterator Ex: to print all the contents of a list Iterator it = linkedList.listIterator(); while(it.hasNext()) { Object obj = it.next(); System.out.println(obj); }
Defining an Iterator - Polymorphism public Iterator listIterator() { return new ListIterator(); } private class ListIterator implements Iterator { … public boolean hasNext(){ public Object next(){ public void remove(){}
All objects have 2 types Apparent Type Actual Type the declared object variable type what it appears to be determines what methods can be called on an object enforced by the compiler Actual Type the constructed variable type what it actually is in memory determines which version of a method to use
Example The toString method: in the Object class, it prints the memory address it’s overridden in the string class, where it prints the text public static void main(String[] args) { String s = "Hello"; printObject(s); Object o = new Object(); printObject(o); } public static void printObject(Object obj) { System.out.println("toString output: " + obj.toString()); What’s the Output?
Why is this important? Look at how we’re using Iterators: For it: Iterator it = linkedList.listIterator(); while(it.hasNext()) { Object obj = it.next(); System.out.println(obj); } For it: What’s the Apparent type? What’s the Actual type? In what class are the next & hasNext methods defined?
What type is E? A generic type What does that mean? it can be anything Why is this useful? we can build classes that process many different types of elements (more on this in HW 2)
Call-by-reference vs. Call-by-value NOTE: Java only uses call-by-value even when objects are passed as parameters Ex, in C++ we can use either
C++ Primitives Example int main() { int a = 5; int b = 5; changeNums(a, b); cout << a; cout << endl; cout << b; } void changeNums(int x, int &y) x = 0; y = 0; Output? 5
Java Primitives Example public static void main(String[] args) { int a = 5; int b = 5; changeNums(a, b); System.out.println(a); System.out.println(b); } public static void changeNums(int x, int y) x = 0; y = 0; Output? 5
Java Objects Example Output? AAA DDD public static void main(String[] args) { CityNode a = new CityNode("AAA", null); CityNode b = new CityNode("BBB", null); changeNodes(a, b); System.out.println(a.city); System.out.println(b.city); } public static void changeNodes(CityNode x, CityNode y) x = new CityNode("CCC", null); y.city = "DDD"; NOTE: When you pass an object to a method, you are passing a copy of the object’s address
Assigning values to objects vs. instance variables CityNode@111111 city: String@222222 null next: x: CityNode@666666 CityNode@111111 value: char[]@333333 3 count: ['A', 'A', 'A'] CityNode a = new CityNode("AAA", null); … changeNodes(a, b); void changeNodes(CityNode x, CityNode y) { x = new CityNode("CCC", null); } city: String@444444 null next: value: char[]@555555 3 count: ['C','C','C']
Assigning values to objects vs. instance variables CityNode@19821f city: String@361e21 null String@1442e2 null next: y: CityNode@19821f value: char[]@166432 3 count: ['B','B','B'] … CityNode b = new CityNode("BBB", null); changeNodes(a, b); void changeNodes(CityNode x, CityNode y) { y.city = "DDD"; } value: char[]@289872 3 count: ['D','D','D']
In Java, how do methods make new objects? Output? Hello Hell They return them Ex, in the String class: substring method returns a new String String s = "Hello"; s.substring(0, 4); System.out.println(s); s = s.substring(0, 4);
Ex: a replaceAll method public class CityListManager { private CityNode head = null; void replaceAll(String oldCity, String newCity) } … CityNode traveller = head; while (traveller != null) { if (traveller.city.equals(oldCity)) traveller.city = newCity; traveller = traveller.next; }
DoublyLinkedList Next lecture: generic doubly linked lists