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

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Why not just use Arrays? Java ArrayLists.
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Section 5 Lists again. Double linked lists – insertion, deletion. Trees.
Chapter 6 Data Types
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
SPL/2010 Pointers and Parameter Passing in C++ 1.
LISTS & TREES Lecture 8 CS2110 – Fall List Overview 2  Purpose  Maintain an ordered set of elements (with possible duplication)  Common operations.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Computers and Programming Lecture 17: Arrays (cont) Professor: Evan Korth New York University.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
1 Lists. 2 Overview Arrays –Random access: –Fixed size: cannot grow on demand after creation:  Characteristics of some applications: –do not need random.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Lists Weiss sec. 6.5 (sort of) ch. 17 (sort of). Arrays Random access a[38] gets 39 th element But fixed size, specified at construction e.g. pickLineFromFile(
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Java Unit 9: Arrays Declaring and Processing Arrays.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
Lecture 12. Searching Algorithms and its analysis 1.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 11 Arrays Continued
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
LISTS Slides of K. Birman, Cornell University. List Overview 2  Purpose  Maintain an ordered collection of elements (with possible duplication)  Common.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
LISTS Lecture 9 CS2110 – Fall Time spent on A2 2  max: 25.4; avg: 5.2 hours, mean: 4.5 hours, min: 0.57 hours.
Section 4 Boxing classes Boxing classes Array initialization Array initialization Lists: recursion and iteration Lists: recursion and iteration.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Working with Loops, Conditional Statements, and Arrays.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
Data Structures Arrays and Lists Part 2 More List Operations.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Introduction to programming in java Lecture 21 Arrays – Part 1.
Object Oriented Programming Lecture 2: BallWorld.
Chapter 4 Linked Lists.
Linked lists.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Object Oriented Programming in java
Arrays in Java.
Linked lists.
Problem 1 Given n, calculate 2n
Presentation transcript:

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

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

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

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

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

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

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

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

(4) Remove list element (Recursion) 1.Assumption: List elements are sorting in ascending order 2.Example: null 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; } } null ListCell :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; }

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. } null ListCell :l

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