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

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

Embed Size (px)

Citation preview

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

Section 4

• Break Statement Usage

• Integer Class and other Wrapper class

• Inside Object Array

• Remove element from a list– Recursive approach– Iterative approach

Page 2: 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

Page 3: 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• 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

Page 4: 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• 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

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

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

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

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

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

(2) Integer class: Class Hierarchy

• Object class is the root of the java class hierarchy.

Object

Animal

Cat

Integer Float Double

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

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

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

(3) Object Array• Storage Structure:

– IntArr[], Integer Array with length 3

Heap:

Integer1

Integer1

Integer1

IntArr[]

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

(3) Create Object Array• IntArr[]= new Integer[3];

Heap:

nullIntArr[] null

null

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

(3) Create Object Array• IntArr[]= new Integer[3];

• For(int I=0; I<3; I++) IntArr[i]=new Integer(i)

Heap:

Integer1

Integer1

Integer1

IntArr[]

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

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

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

(4) Remove element from list

class ListCell {

//data

protected Object datum;

protected ListCell next;

//methods

public ListCell(Object o, ListCell n)

……

}

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

(4) Remove list element (Recursion)

1. Assumption: List elements are sorting in ascending order

2. Example:

1510

2025

null

ListCell:l

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

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

}

1510

2025

nullListCell

:l

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

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;

}

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

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.

}

1510

2025

nullListCell

:l

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

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…

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

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;

}