Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 214 – Computer Science I More on Linked Lists

Similar presentations


Presentation on theme: "CSE 214 – Computer Science I More on Linked Lists"— Presentation transcript:

1 CSE 214 – Computer Science I More on Linked Lists

2 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

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

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

5 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); }

6 Defining an Iterator - Polymorphism
public Iterator listIterator() { return new ListIterator(); } private class ListIterator implements Iterator { … public boolean hasNext(){ public Object next(){ public void remove(){}

7 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

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

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

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

11 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

12 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

13 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

14 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

15 Assigning values to objects vs. instance variables
city: null next: x: value: 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: null next: value: 3 count: ['C','C','C']

16 Assigning values to objects vs. instance variables
city: null null next: y: value: 3 count: ['B','B','B'] CityNode b = new CityNode("BBB", null); changeNodes(a, b); void changeNodes(CityNode x, CityNode y) { y.city = "DDD"; } value: 3 count: ['D','D','D']

17 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);

18 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; }

19 DoublyLinkedList Next lecture: generic doubly linked lists


Download ppt "CSE 214 – Computer Science I More on Linked Lists"

Similar presentations


Ads by Google