CPSC 233 Tutorial
description
Transcript of CPSC 233 Tutorial
![Page 1: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/1.jpg)
CPSC 233 TutorialXin Feb 7, 2011
![Page 2: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/2.jpg)
James Tam
Assignment 3•Due on Friday, Feb 18
•Garbage collection•Runtime object•Array•Linked list/chain
![Page 3: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/6.jpg)
Array A structure with a fixed number of elements int [] arr = new int[10]; arr[0] = 100;
![Page 7: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/8.jpg)
James Tam
List Elements: Nodes
Node
Data (data)Pointer/reference
(connector)
Data Ptr Data Ptr Data Ptr
Head
![Page 9: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/9.jpg)
James Tam
List Operations: Linked Lists (Insertion)
•Graphical illustration of the algorithm:
NULL
LIST
NEW ELEMENT
Temp
![Page 10: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/10.jpg)
James Tam
List Operations: Linked Lists (Insertion: 2)
•Graphical illustration of the algorithm:
NULL
LIST
NEW ELEMENT
Temp
![Page 11: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/11.jpg)
James Tam
List Operations: Linked Lists (Insertion: 3)
•Graphical illustration of the algorithm:
NULL
LIST
NEW ELEMENT
Temp
![Page 12: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/12.jpg)
James Tam
List Operations: Linked Lists (Removing Elements)
•Graphical illustration of the algorithm
NULL
LIST Remove
![Page 13: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/13.jpg)
James Tam
List Operations: Linked Lists (Removing Elements: 2)
•Graphical illustration of the algorithm
NULL
LIST Remove
CurrentPrevious
![Page 14: CPSC 233 Tutorial](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062305/56815f61550346895dce4aba/html5/thumbnails/15.jpg)
James Tam
Example -- Book Inventory
•UML