150 likes | 282 Views
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
E N D
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 Foof = 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 -- example class Runtime_foo { public void finalize () { System.out.println("going"); } } class Runtime_driver { public static void main (String [] args) { Runtime_foof = 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 example public class StringArray { public static void main (String [] args) { String [] myStringArray = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; for (inti = 0; i < myStringArray.length; i++) { System.out.println(myStringArray[i]); } } }
Head Data Data Data Ptr Ptr Ptr Pointer/reference (connector) Data (data) Node List Elements: Nodes
LIST NULL NEW ELEMENT List Operations: Linked Lists (Insertion) • Graphical illustration of the algorithm: Temp
LIST NULL NEW ELEMENT List Operations: Linked Lists (Insertion: 2) • Graphical illustration of the algorithm: Temp
List Operations: Linked Lists (Insertion: 3) • Graphical illustration of the algorithm: Temp LIST NULL NEW ELEMENT
List Operations: Linked Lists (Removing Elements) • Graphical illustration of the algorithm Remove LIST NULL
List Operations: Linked Lists (Removing Elements: 2) • Graphical illustration of the algorithm Remove LIST NULL Previous Current
List Operations: Linked Lists (Removing Elements: 2) • Graphical illustration of the algorithm Remove LIST NULL Previous Current Node to be removed has been bypassed (effectively deleted from the list)