190 likes | 200 Views
Learn how to use break statements, work with the Integer class, create and manipulate object arrays, and remove elements from a list in Java.
E N D
Section 4 • Break Statement Usage • Integer Class and other Wrapper class • Inside Object Array • Remove element from a list • Recursive approach • Iterative approach
(1) Break Statement Usage • Loop Break: myArray[]: 1-d integer array for (int i=0; i < myArray.length; i++) { if (myArray[i] = = 0) { break; // break out of for loop } } // execution resumes at following statement on break
(1) Break Statement Usage • Label Break: myArray[][]: 2-d integer array breakout: for (int i=0; i < myArray.length; i++) { for (int j=0; j < myArray[i].length; j++) { if (myArray[i][j] = = 0) { break breakout; // break out of for loop } } } // execution resumes at following statement on break
(1) Break Statement Usage • Not recommend label break, not a good programming style. • Label break and loop break can be eliminated. for (int i=0; i < myArray.length && myArray[i]!=0; i++) { } // execution resumes
(2) Integer class • int is java built-in primitive data type. • The Integer class wraps a value of the primitive type int in an object. • An object of type Integer contains a single field whose type is int.
(2) Integer class: API • Create an Integer object. • Integer IntObj = new Integer(5); • Retrieve an Integer object’s value. • int n = IntObj.intValue( ); • Parse a integer from a String. • int n = Interger.parseInt(“1234”);
Object Animal Cat Integer Float Double (2) Integer class: Class Hierarchy • Object class is the root of the java class hierarchy.
Integer Object Upcasting (Implicit casting): Object Obj=new Integer(10); Downcasting (explicit casting): Integer IntObj=(Integer) Obj; System.out.println(InObj); System.out.println (InObj.intValue()); (2) Integer class: Casting • Casting ( the same thing with different type) • Upcasting • Downcasting
(3) Object Array • Storage Structure: • IntArr[], Integer Array with length 3 Heap: Integer1 IntArr[] Integer1 Integer1
(3) Create Object Array • IntArr[]= new Integer[3]; Heap: null IntArr[] null null
(3) Create Object Array • IntArr[]= new Integer[3]; • For(int I=0; I<3; I++) IntArr[i]=new Integer(i) Heap: Integer1 IntArr[] Integer1 Integer1
(3) Create Object Array • Summary: • When creating an array of objects, each element is a reference variable with a default value of null. • Need to explicitly create an obj for each element.
(4) Remove element from list class ListCell { //data protected Object datum; protected ListCell next; //methods public ListCell(Object o, ListCell n) …… }
15 10 20 25 null (4) Remove list element (Recursion) • Assumption: List elements are sorting in ascending order • Example: ListCell:l
25 20 15 null 10 ListCell:l listcellDelete(Integer num, ListCell l){ if (l = = null) return null; if (no more smaller value) return l; else{ suppose l is [f, n]; if num = = f return l.getnext(); else l.setNext( listcellDelete(c, l.getNext())) return l; } }
public static ListCell deleteRec(Integer obj, ListCell l) { if (l == null) return null; int lvalue=((Integer)l.getDatum).intValue(); int ovalue=obj.intValue(); if (lvalue > ovalue) return l; /* not in list */ if (lvalue==ovalue) /* delete this cell*/ return l.getNext(); l.setNext(deleteRec(obj, l.getNext())); /*delete from the rest of the list*/ return l; }
25 20 15 null 10 ListCell:l • listcellDelete(Integer num, ListCell l){ • Iterative approach: • Locate the predecessor of the cell that we need to delete. (Scan) • Update the predecessor’s next pointer point to the deleted cell’s next point. • }
public static ListCell deleteIter(Integer obj, ListCell l) { //check three simple cases first if (l == null) return null; int ovalue=obj.intValue(); if (((Integer)l.getDatum).intValue()==ovalue) { return l.getNext(); } if (((Integer)l.getDatum).intValue()>ovalue) { return l; } //Cont…
public static ListCell deleteIter(Integer obj, ListCell l) { //Cont. //current and scout are cursors into list. //both advance in lock step, scout is one cell ahead //stop if scout points to cell containing obj or if scout is null ListCell current = l; ListCell scout = l.getNext(); while ((scout != null) && ((Integer)scout.getDatum()).intValue() < ovalue){ current = scout; scout = scout.getNext(); } //if scout is not null, we have found a cell containing obj if (scout != null && ((Integer)scout.getDatum()).intValue() = = ovalue)) current.setNext(scout.getNext()); return l; }