CPSC 233 TutorialXin Feb 7, 2011
James Tam
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 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
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.
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(); }}
Array A structure with a fixed number of elements int [] arr = new int[10]; arr[0] = 100;
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]);}
}}
James Tam
List Elements: Nodes
Node
Data (data)Pointer/reference
(connector)
Data Ptr Data Ptr Data Ptr
Head
James Tam
List Operations: Linked Lists (Insertion)
•Graphical illustration of the algorithm:
NULL
LIST
NEW ELEMENT
Temp
James Tam
List Operations: Linked Lists (Insertion: 2)
•Graphical illustration of the algorithm:
NULL
LIST
NEW ELEMENT
Temp
James Tam
List Operations: Linked Lists (Insertion: 3)
•Graphical illustration of the algorithm:
NULL
LIST
NEW ELEMENT
Temp
James Tam
List Operations: Linked Lists (Removing Elements)
•Graphical illustration of the algorithm
NULL
LIST Remove
James Tam
List Operations: Linked Lists (Removing Elements: 2)
•Graphical illustration of the algorithm
NULL
LIST Remove
CurrentPrevious
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)
James Tam
Example -- Book Inventory
•UML
Top Related