Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

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

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

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

5 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

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

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

8 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

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

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

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

12 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

13 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

14 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

15 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 40725 3.58

16 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 40725 3.57 Jane Jones 58821 3.72

17 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

18 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

19 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

20 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

21 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

22 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

23 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


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

Similar presentations


Ads by Google