1 / 19

Java Programming: Break Statement, Integer Class, Object Array, Remove Element from List

Learn how to use break statements, work with the Integer class, create and manipulate object arrays, and remove elements from a list in Java.

bpounds
Download Presentation

Java Programming: Break Statement, Integer Class, Object Array, Remove Element from List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Object Animal Cat Integer Float Double (2) Integer class: Class Hierarchy • Object class is the root of the java class hierarchy.

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

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

  10. (3) Create Object Array • IntArr[]= new Integer[3]; Heap: null IntArr[] null 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[] 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. 15 10 20 25 null (4) Remove list element (Recursion) • Assumption: List elements are sorting in ascending order • Example: ListCell:l

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

  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. 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. • }

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

More Related