CSE 214 – Computer Science I More on Linked Lists

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Java Review Interface, Casting, Generics, Iterator.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Arrays And ArrayLists - S. Kelly-Bootle
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Java and C++, The Difference An introduction Unit - 00.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
More on Arrays Review of Arrays of ints, doubles, chars
Java: Base Types All information has a type or class designation
Examples of Classes & Objects
Lecture 5: Some more Java!
Chapter 8 Classes and Objects
Continuing Chapter 11 Inheritance and Polymorphism
C++ -> Java - A High School Teacher's Perspective
Array traversals, text processing
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Instance Method Review - CIS 1068 Program Design and Abstraction
CSC240 Computer Science III
Building Java Programs
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Testing and debugging A short interlude 2-Dec-18.
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Dynamic Data Structures and Generics
Building Java Programs
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
slides created by Ethan Apter
Why did the programmer quit his job?
CSE 143 Lecture 5 References and Linked Nodes
Dynamic Data Structures and Generics
Suggested self-checks: Section 7.11 #1-11
Arrays Arrays A few types Structures of related data items
Testing and debugging A short interlude 17-Apr-19.
Java’s Central Casting
List Iterator Implementation
Building Java Programs
Software Design Lecture : 39.
Lecture 6: References and linked nodes reading: 16.1
CSE 143 Lecture 7 References and Linked Nodes reading: 16.1
Methods/Functions.
CIS 110: Introduction to Computer Programming
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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