CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
Building Java Programs Chapter 16 Linked Lists. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[]
1 Reference semantics. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b.
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Object Oriented Programming
Building Java Programs Chapter 16
CSCI 162 – Introduction to Programming II William Killian
Building Java Programs
Building Java Programs
Lecture 13: More Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 16
CSc 110, Autumn 2017 Lecture 23: lists as Parameters
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Road Map CS Concepts Data Structures Java Language Java Collections
Array traversals, text processing
Linked node problem 3 What set of statements turns this picture:
Building Java Programs
Building Java Programs
Pass by Reference, const, readonly, struct
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Autumn 2016 Lecture 19: lists as Parameters
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from
CSE 143 Lecture 2 Collections and ArrayIntList
Why did the programmer quit his job?
Building Java Programs
Parameter Passing in Java
CSE 214 – Computer Science I More on Linked Lists
slides created by Marty Stepp and Hélène Martin
CSc 110, Spring 2018 Lecture 23: lists as Parameters
Lecture 10: Arrays AP Computer Science Principles
Simulating Reference Parameters in C
Lecture 12: 2D Arrays AP Computer Science Principles
Lecture 10: Arrays II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
References and objects
Recall: stacks and queues
Why did the programmer quit his job?
slides created by Marty Stepp
CSE 143 Lecture 5 References and Linked Nodes
Building Java Programs Chapter 16
Building Java Programs
Road Map CS Concepts Data Structures Java Language Java Collections
Recall: stacks and queues
Linked Lists based on slides created by Ethan Apter
Building Java Programs
Building Java Programs
slides created by Marty Stepp
Lecture 7: Linked List Basics reading: 16.2
Lecture 6: References and linked nodes reading: 16.1
CSE 143 Lecture 7 References and Linked Nodes reading: 16.1
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
slides created by Marty Stepp
Linked Lists and Loops based on slides by Ethan Apter
Building Java Programs
Lecture 6: References and linked nodes reading: 16.1
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1 slides created by Marty Stepp http://www.cs.washington.edu/143/

A swap method? Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b = 35; // swap a with b swap(a, b); System.out.println(a + " " + b); } public static void swap(int a, int b) { int temp = a; a = b; b = temp;

Value semantics value semantics: Behavior where values are copied when assigned to each other or passed as parameters. When one primitive is assigned to another, its value is copied. Modifying the value of one variable does not affect others. int x = 5; int y = x; // x = 5, y = 5 y = 17; // x = 5, y = 17 x = 8; // x = 8, y = 17

Reference semantics reference semantics: Behavior where variables actually store the address of an object in memory. When one reference variable is assigned to another, the object is not copied; both variables refer to the same object. int[] a1 = {4, 5, 2, 12, 14, 14, 9}; int[] a2 = a1; // refers to same array as a1 a2[0] = 7; System.out.println(a1[0]); // 7 a1 index 1 2 3 4 5 6 value 12 14 9 index 1 2 3 4 5 6 value 7 12 14 9 a2

References and objects In Java, objects and arrays use reference semantics. Why? efficiency. Copying large objects slows down a program. sharing. It's useful to share an object's data among methods. DrawingPanel panel1 = new DrawingPanel(80, 50); DrawingPanel panel2 = panel1; // same window panel2.setBackground(Color.CYAN); panel1 panel2

Objects as parameters When an object is passed as a parameter, the object is not copied. The parameter refers to the same object. If the parameter is modified, it will affect the original object. public static void main(String[] args) { DrawingPanel window = new DrawingPanel(80, 50); window.setBackground(Color.YELLOW); example(window); } public static void example(DrawingPanel panel) { panel.setBackground(Color.CYAN); window Note: This is also the reason that it works when you pass the Graphics g as a parameter to a method, because it is drawing with the same pen object onto the same window. panel

References as fields Objects can store references to other objects as fields. Example: Homework 2 (HTML Validator) HtmlValidator stores a reference to a Queue the Queue stores many references to HtmlTag objects each HtmlTag object stores a reference to its element String private Queue<HtmlTag> queue; ... HtmlValidator back ... front Queue private String element; ... HtmlTag private String element; ... HtmlTag l m t h String y d o b String

References to same type What would happen if we had a class that declared one of its own type as a field? public class StrangeObject { String name; StrangeObject other; } Will this compile? If so, what is the behavior of the other field? What can it do? If not, why not? What is the error and the reasoning behind it?

Linked data structures All of the collections we will use and implement in this course use one of the following two underlying data structures: an array to store all elements of the collection ArrayList, Stack, HashSet, HashMap a set of linked objects, each storing one element, that contain references to each other LinkedList, TreeSet, TreeMap index 1 2 3 value 42 -3 17 9 data next 42 data next -3 data next 17 data next 9 null front

A list node class Each list node object stores: public class ListNode { int data; ListNode next; } Each list node object stores: one piece of integer data a reference to another list node ListNodes can be "linked" into chains to store a list of values: data next 42 data next -3 data next 17 data next 9 null

List node w/ constructor public class ListNode { int data; ListNode next; public ListNode(int data) { this.data = data; this.next = null; } public ListNode(int data, ListNode next) { this.next = next; Exercise: Write the code to produce the list on the previous slide.

Linked node problem 1 What set of statements turns this picture: Into this? data next 10 data next 20 list data next 10 data next 20 data next 30 list

Linked node problem 2 What set of statements turns this picture: Into this? data next 10 data next 20 list data next 30 data next 10 data next 20 list

Linked node problem 3 What set of statements turns this picture: Into this? data next 10 data next 20 list1 data next 30 data next 40 list2 data next 10 data next 30 data next 20 list1 data next 40 list2

Linked node problem 4 What set of statements turns this picture: Into this? data next 10 data next 990 list ... data next 10 data next 990 data next 1000 list ...