Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 4 Break Statement Usage Integer Class and other Wrapper class Inside Object Array Remove element from a list –Recursive approach –Iterative approach.

Similar presentations


Presentation on theme: "Section 4 Break Statement Usage Integer Class and other Wrapper class Inside Object Array Remove element from a list –Recursive approach –Iterative approach."— Presentation transcript:

1 Section 4 Break Statement Usage Integer Class and other Wrapper class Inside Object Array Remove element from a list –Recursive approach –Iterative approach

2 (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

3 (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

4 (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

5 (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.

6 (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”);

7 (2) Integer class: Class Hierarchy Object class is the root of the java class hierarchy. ObjectAnimalCatIntegerFloatDouble

8 (2) Integer class: Casting Casting ( the same thing with different type) –Upcasting –Downcasting Object Integer Upcasting (Implicit casting): Object Obj=new Integer(10); Downcasting (explicit casting): Integer IntObj=(Integer) Obj; System.out.println(InObj); System.out.println (InObj.intValue());

9 (3) Object Array Storage Structure: –IntArr[], Integer Array with length 3 Heap: Integer1 IntArr[]

10 (3) Create Object Array IntArr[]= new Integer[3]; Heap: null IntArr[] null

11 (3) Create Object Array IntArr[]= new Integer[3]; For(int I=0; I<3; I++) IntArr[i]=new Integer(i) Heap: Integer1 IntArr[]

12 (3) Create Object Array Summary: 1.When creating an array of objects, each element is a reference variable with a default value of null. 2.Need to explicitly create an obj for each element.

13 (4) Remove element from list class ListCell { //data protected Object datum; protected ListCell next; //methods public ListCell(Object o, ListCell n) …… }

14 (4) Remove list element (Recursion) 1.Assumption: List elements are sorting in ascending order 2.Example: 15102025 null ListCell:l

15 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; } } 15 10 20 25 null ListCell :l

16 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; }

17 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. } 15 10 20 25 null ListCell :l

18 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…

19 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; }


Download ppt "Section 4 Break Statement Usage Integer Class and other Wrapper class Inside Object Array Remove element from a list –Recursive approach –Iterative approach."

Similar presentations


Ads by Google