Arrays Continued - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Arrays Continued

Description:

e.g. s.indexOf('a') returns 3. int indexOf(char a, int index) and int indexOf(String sub) ... e.g. s.substring(2) returns String 'eat' ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 22
Provided by: muX
Category:

less

Transcript and Presenter's Notes

Title: Arrays Continued


1
Arrays Continued
  • Objectives
  • Learn how to manipulate strings
  • Use String methods appropriately
  • Understand objects in arrays
  • Use polymorphism in arrays

2
The Importance of String
  • Strings are everywhere in programming
  • In user interface
  • A significant part of user input is through
    strings
  • Output also often involves strings
  • Program interfaces are also becoming more
    text-based
  • XML

3
In The Textbook
  • Strings
  • p6365 first introduction (Lesson 3.2)
  • p315319 more String operations
  • Arrays of objects
  • p334336 Lesson 10.5

4
Strings in Java
  • Each string is an object of String
  • The String class has variables and methods
  • Each string composes an ordered list of
    characters (note character is denoted by )
  • Each String object has methods that manipulate
    the ordered list of characters

5
Escape Characters
  • Some special characters cannot appear in a string
    for various reasons
  • As Strings are quoted by , to include , one
    needs to use \
  • New line character and tab are represented by \n
    and \t
  • \ itself becomes \\

6
We Already Know Some Things About String
  • Declaration and instantiation
  • Initialization
  • Concatenation
  • The operator
  • Precedence of concatenation
  • concatenation has the same precedence as addition
  • Examples n34, n34, 34n

7
Methods of String (int and char Returns)
  • The length() method
  • Returns the number of characters, including
    spaces (Note in array length is an attribute)
  • char charAt(int anIndex) returns the character
    at anIndex (starting at 0)
  • e.g. String sgreat s.charAt(2) returns e
  • int indexOf(char a) returns the index of first
    occurrence of a, -1 if a is not part of string
  • e.g. s.indexOf(a) returns 3
  • int indexOf(char a, int index) and int
    indexOf(String sub)
  • s.indexOf(e,3) returns 1

8
String Comparison Methods (int and boolean
Returns)
  • int compareTo(String a)
  • lexicographically compare with a
  • returns 0 if equal, a negative number if I am
    less than a and positive number if a is less than
    myself
  • boolean equals(String a)
  • Returns true if a is equal to me in content,
    false otherwise
  • boolean equalsIgnorCase(String a)

9
More String Methods (String Returns)
  • String substring(int beginIndex) returns
    substring from beginIndex till the end of the
    string
  • e.g. s.substring(2) returns String eat
  • String substring(int start, int end) return
    substring between indices start and end

10
More String Methods (String returns)
  • String toLowerCase()
  • String toUpperCase()
  • String trim() returns string without leading and
    trailing spaces
  • Note if the return is of String type, the
    original string is not changed

11
Exercises
  • 1. What does the following code do?

String original "Hi there!" String word
""   // Visit all the characters in the
string for (int i 0 i lt original.length()
i)   // Or stop when a space is found if
(original.charAt(i) ' ') break   //
Add the non-space character to the word word
original.charAt(i)
12
Exercises
  • 2. What does the following code do?

String strThe rain in Spain falls mainly on
the plain int begin0 while (beginltstr.length()
) int endstr.indexOf( ,begin) if
(end-1) endstr.length() String
wordstr.substring(begin,end) System.out.println
(word) beginend1
13
Want to Know More String Methods?
  • Refer to the documentation!

14
Working with Arrays of Objects
  • Polymorphism, Casting, and instanceof
  • It is quite common to declare and instantiate an
    array of some interface type.
  • For instance, below is code that reserves 10
    cells for shapes
  • Shape shapes new Shape10

15
Storing Different Objects in One Array
  • We can now store in this array instances of any
    class that implements Shape, such as Rect,
    Circle, or Wheel, as follows
  • As long as we send Shape messages to the elements
    of this array, we can ignore the fact that they
    are of different concrete classes (thats what
    polymorphism is all about).

shapes0 new Rect(20, 20, 40, 40) // Cell 0
refers to a Rect shapes1 new Circle(100, 100,
20) // Cell 1 refers to a Circle shapes2
new Wheel(200, 200, 20, 6) // Cell 2 refers to
a Wheel
16
Sending One Common Message
  • Let us now draw all the shapes in the array

Pen pen new StandardPen() for (int i 0 i lt
3 i) shapesi.draw(pen)
17
Sending a Message Specific to One Class
  • Let us assume that we know the position of the
    wheel object in the array (in our example, it's
    at position 2).
  • Then, to set its spokes to 5, we perform the
    following steps
  • Access the array element with the subscript.
  • Cast the element, which is masquerading as a
    Shape, to a Wheel.
  • Send the setSpokes(5) message to the result.
  • Here is the code
  • ((Wheel) shapes2).setSpokes(5)

18
Casting
  • Note the use of parentheses to override the
    precedence of the method selector, which would
    otherwise be run before the cast operation.
  • Failure to cast in this code causes a
    compile-time error.
  • Suppose we don't know the position of a wheel in
    the array of shapes, but we wish to set the
    spokes of each wheel to 5.
  • We must first determine that a shape is a wheel
    before casting, and Java's instanceof operator
    comes to our rescue.

19
Using instanceof
  • Here is a loop that solves the problem
  • We only send setSpokes() to a Wheel

for (int i 0 i lt shapes.length i) if
(shapesi instanceof Wheel) ((Wheel)
shapesi).setSpokes(5)
20
Summary of Using Objects of Different Types in
Array
  • When the element type of an array is a reference
    type or interface, objects of those types or any
    subtype (subclass or implementing class) can be
    directly inserted into the array.
  • After accessing an object in an array, care must
    be taken to send it the appropriate messages or
    to cast it down to a type that can receive the
    appropriate messages
  • The beauty of polymorphism lies in the ability of
    the client to send one message to the array and
    the actual binding of methods to object happens
    at run-time

21
Exercises
  • Exercise 10.5 on Page 336
Write a Comment
User Comments (0)
About PowerShow.com