COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.

Slides:



Advertisements
Similar presentations
Chapter 24 Lists, Stacks, and Queues
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 9: Data Structures I
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CISH 4960 Introduction to Programming Lecture 101 Introduction to Programming Lecture 10 Recursion/Data Structures Tom Blough
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class.
Chapter 12: Data Structures
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Grade 12 Computer Studies HG
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Data structures Abstract data types Java classes for Data structures and ADTs.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stacks And Queues Chapter 18.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Click to edit Master text styles Stacks Data Structure.
Collections (Stack, queue, tree, graph). References as Links References can be used to create a variety of linked structures, such as a linked list.
Java Software Solutions Foundations of Program Design Seventh Edition
Data Structure By Amee Trivedi.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 12 – Data Structures
Going further Enumerated types Recursion Collections.
Chapter 12: Data Structures
Data Structures and Database Applications Abstract Data Types
Chapter 12 Collections.
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Chapter 13 Collections.
Chapter 12 Collections.
Introduction to Data Structure
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Presentation transcript:

COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer Examples in chapters 9 and 13 Data Structures

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Strings *A string is a sequence of characters that is treated as a single value. *Instances of the String class are used to represent strings in Java. *We access individual characters of a string by calling the charAt method of the String object. the first character’s index is 0, the second is 1, and so on.

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Other String Methods *String toLowerCase() *String toUpperCase() *boolean equalsIgnoreCase ( String other) *int compareTo( String other) *String replace( char oldChar, char newChar);

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. String Comparison *String comparison may be done in several ways. The methods equals and equalsIgnoreCase compare string values; one is case-sensitive and one is not. The method compareTo returns an int value: Zero (0) if the strings are equal. A negative integer if the first string is less than the second. A positive integer if the first string is greater than the second.

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. String Equality *Comparing String objects is similar to comparing other objects. *The equality test (==) is true if the contents of the variables are the same. For a reference data type, the equality test is true if both variables refer to the same object, because they both contain the same address. *The equals method is true if the String objects to which the two variables refer contain the same string value. the same sequence of characters

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Equality Testing *The difference between the equality test and the equals method.

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Equality Testing

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Equality Testing for Strings *As long as a new String object is created using the new operator, the rule for comparing objects applies to comparing strings. String str = new String (“Java”); *If the new operator is not used, a String reference refers to the literal string constant. String str = “Java”; There will be only one copy of any literal string

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Memory Diagram *The difference between using and not using the new operator for String.

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Immutability *When a String object is created, it cannot be changed. Look at the documentation - there are no mutator methods We say Strings are immutable. *Manipulating the content of a String requires the creation of a new String object. If you are doing a lot of manipulation this is not very efficient.

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. String Tokenizer *A StringTokenizer object can be used to break a string up into smaller substrings (tokens) *By default, the tokenizer separates the string at white space *The default StringTokenizer constructor takes the original string to be separated as a parameter StringTokenizer st = new StringTokenizer( text); *An alternate constructor allows you to specify different separators (delimiters). StringTokenizer st = new StringTokenizer( text, delims);

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. String Tokenizer Methods *The hasMoreTokens method returns true if there are more tokens in the string. *Each call to the nextToken method returns the next token in the string. *If you need to know how many tokens there are, you can call countTokens

COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Data Structures *Array is only one possible structure for storing collections

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Static vs. Dynamic Structures *A static data structure has a fixed size *Arrays are static; once you define the number of elements it can hold, the number doesn’t change An ArrayList can change its size but it really just hides the work involved in changing the size of a static structure *A dynamic data structure grows and shrinks at execution time as required by its contents *A dynamic data structure is implemented using links

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Object References *Recall that an object reference is a variable that stores the address of an object A reference may be called a pointer *References can be depicted graphically: student John Smith

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. References as Links *Object references can be used to create links between objects *Suppose a Student class contains a reference to another Student object John Smith Jane Jones

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. References as Links *References can be used to create a variety of linked structures, such as a linked list: *This is called a LinkedList LinkedList class has same methods as ArrayList studentList

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Abstract Data Types *Collections can be implemented in many different ways *An abstract data type (ADT) is an organized collection of information and a set of operations used to manage that information *The set of operations defines the interface to the ADT *As long as the ADT fulfills the promises of the interface, it doesn't really matter how the ADT is implemented *Objects are a perfect programming mechanism to create ADTs because their internal details are encapsulated

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Other Dynamic List Representations *It may be convenient to implement as list as a doubly linked list, with next and previous references list

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Queues *A queue is similar to a list but adds items only to the rear of the list and removes them only from the front *It is called a FIFO data structure: First-In, First-Out *Analogy: a line of people at a bank teller’s window enqueue dequeue

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Stacks *A stack ADT is also linear, like a list or a queue *Items are added and removed from only one end of a stack *It is therefore LIFO: Last- In, First-Out *Analogies: a stack of plates in a cupboard, a stack of bills to be paid, or a stack of hay bales in a barn poppush

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Trees and Binary Trees *A tree is a non-linear data structure that consists of a root node and potentially many levels of additional nodes that form a hierarchy Each node can be linked to at least two other nodes Nodes that have no children are called leaf nodes Nodes except for the root and leaf nodes are called internal nodes

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Trees and Binary Trees *A binary tree is defined recursively. Either it is empty (the base case) or it consists of a root and two subtrees, each of which is a binary tree *Binary trees and trees typically are represented using references as dynamic links, though it is possible to use fixed representations like arrays