cpsc 233 tutorial

15
CPSC 233 Tutorial Xin Feb 7, 2011

Upload: stuart

Post on 16-Feb-2016

48 views

Category:

Documents


0 download

DESCRIPTION

CPSC 233 Tutorial. Xin Feb 7, 2011. Assignment 3. Due on Friday, Feb 18 Garbage collection Runtime object Array Linked list/chain. Garbage collector. In Foo f = new Foo (); , f is a “reference” to the object - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CPSC 233 Tutorial

CPSC 233 TutorialXin Feb 7, 2011

Page 2: CPSC 233 Tutorial

James Tam

Assignment 3•Due on Friday, Feb 18

•Garbage collection•Runtime object•Array•Linked list/chain

Page 3: CPSC 233 Tutorial

Garbage collector In Foo f = new Foo();, f is a “reference” to the object If later you write f = new Foo(); or f = null; the

previously created object is not referenced any more It becomes a garbage Java will use a collector to free the memory occupied

by garbage When a garbage is freed, a finalize method is called

if you implement it for the class finalize() is used to free resources, such as files and

network connections

Page 4: CPSC 233 Tutorial

Runtime An object allows an application to interface

with the running environment getRuntime()

return the current runtime. gc()

Run the garbage collector. runFinalization()

Runs the finalization methods of any objects pending finalization.

Page 5: CPSC 233 Tutorial

Runtime -- exampleclass Runtime_foo{ public void finalize () { System.out.println("going"); }}

class Runtime_driver{ public static void main (String [] args) { Runtime_foo f = new Runtime_foo (); f = null; Runtime rt = Runtime.getRuntime(); rt.gc(); // calls finalize rt.runFinalization(); }}

Page 6: CPSC 233 Tutorial

Array A structure with a fixed number of elements int [] arr = new int[10]; arr[0] = 100;

Page 7: CPSC 233 Tutorial

Array – an examplepublic class StringArray{ public static void main (String [] args) {

String [] myStringArray = { "zero", "one", "two", "three", "four", "five", "six",

"seven", "eight", "nine"};

for (int i = 0; i < myStringArray.length; i++){ System.out.println(myStringArray[i]);}

}}

Page 8: CPSC 233 Tutorial

James Tam

List Elements: Nodes

Node

Data (data)Pointer/reference

(connector)

Data Ptr Data Ptr Data Ptr

Head

Page 9: CPSC 233 Tutorial

James Tam

List Operations: Linked Lists (Insertion)

•Graphical illustration of the algorithm:

NULL

LIST

NEW ELEMENT

Temp

Page 10: CPSC 233 Tutorial

James Tam

List Operations: Linked Lists (Insertion: 2)

•Graphical illustration of the algorithm:

NULL

LIST

NEW ELEMENT

Temp

Page 11: CPSC 233 Tutorial

James Tam

List Operations: Linked Lists (Insertion: 3)

•Graphical illustration of the algorithm:

NULL

LIST

NEW ELEMENT

Temp

Page 12: CPSC 233 Tutorial

James Tam

List Operations: Linked Lists (Removing Elements)

•Graphical illustration of the algorithm

NULL

LIST Remove

Page 13: CPSC 233 Tutorial

James Tam

List Operations: Linked Lists (Removing Elements: 2)

•Graphical illustration of the algorithm

NULL

LIST Remove

CurrentPrevious

Page 14: CPSC 233 Tutorial

James Tam

List Operations: Linked Lists (Removing Elements: 2)

•Graphical illustration of the algorithm

NULL

LIST Remove

CurrentPrevious

Node to be removed has been bypassed (effectively deleted from the list)

Page 15: CPSC 233 Tutorial

James Tam

Example -- Book Inventory

•UML