Title: Arrays Continued
1Arrays Continued
- Objectives
- Learn how to manipulate strings
- Use String methods appropriately
- Understand objects in arrays
- Use polymorphism in arrays
2The 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
3In The Textbook
- Strings
- p6365 first introduction (Lesson 3.2)
- p315319 more String operations
- Arrays of objects
- p334336 Lesson 10.5
4Strings 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
5Escape 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 \\
6We 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
7Methods 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)
9More 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
10More 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
11Exercises
- 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)
12Exercises
- 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
13Want to Know More String Methods?
- Refer to the documentation!
14Working 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
15Storing 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
16Sending 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)
17Sending 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)
18Casting
- 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.
19Using 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)
20Summary 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
21Exercises
- Exercise 10.5 on Page 336