Title: Break Statement Usage
1Section 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
myArray 1-d integer array for (int i0 i lt
myArray.length i) if (myArrayi
0) break // break out of for
loop // execution resumes at
following statement on break
3(1) Break Statement Usage
myArray 2-d integer array breakout
for (int i0 i lt myArray.length i)
for (int j0 j lt myArrayi.length j)
if (myArrayij 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 i0 i lt myArray.length
myArrayi!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 objects 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.
8(2) Integer class Casting
- Casting ( the same thing with different type)
- Upcasting
- Downcasting
9(3) Object Array
- Storage Structure
- IntArr, Integer Array with length 3
Heap
Integer1
IntArr
Integer1
Integer1
10(3) Create Object Array
Heap
null
IntArr
null
null
11(3) Create Object Array
- IntArr new Integer3
- For(int I0 Ilt3 I) IntArrinew Integer(i)
Heap
Integer1
IntArr
Integer1
Integer1
12(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.
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)
- Assumption List elements are sorting in
ascending order - Example
ListCelll
15listcellDelete(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
16public static ListCell deleteRec(Integer obj,
ListCell l) if (l null) return null int
lvalue((Integer)l.getDatum).intValue() int
ovalueobj.intValue() if (lvalue gt ovalue)
return l / not in list / if (lvalueovalue)
/ 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 predecessors next pointer point to
the deleted cells next point.
18public static ListCell deleteIter(Integer obj,
ListCell l) //check three simple cases
first if (l null) return null int
ovalueobj.intValue() if (((Integer)l.getDatum).
intValue()ovalue) return l.getNext()
if (((Integer)l.getDatum).intValue()gtovalue)
return l //Cont
19public 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() lt
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