Java Reference
In-Depth Information
￿ s.setCharAt(3,'a');
Changes the character at position 3 to
. The method can
only be applied to objects of type StringBuffer .
Removes the character at position 3. The characters after
position 3 are shifted one position to the left. The method can only be applied to
objects of type StringBuffer .
Creates a new StringBuffer
StringBuffer s = new StringBuffer("John");
object with text John .
￿ s.append("Bob");
Appends Bob to the StringBuffer .
7.8 Important Points
1. The classes Integer , Character , Double ,and String , among others, are immutable.
In other words, we cannot modify objects that belong to these classes. If we try to
do so, then new objects are created. As a consequence, if a method tries to modify a
formal parameter object that belongs to an immutable class, the changes will not be
seen by the calling method.
2. If we want to modify the characters of a String , then we need to create a
StringBuffer object instead.
3. An ArrayList takes as input a type parameter. This parameter must be the name
of a class and cannot be the name of a primitive type (e.g., int , double ,or char ).
To allow an ArrayList of primitive type, the wrapper classes Byte , Short , Integer ,
Long , Float , Double , Character ,and Boolean are supported by Java.
4. If we want to associate a value with the constants of an enum type, then we need to
include a private constructor and a getter method.
5. Do not use the set method to add new values to an ArrayList . Instead, use the add
method. The set method should be used to change existing values of an ArrayList .
6. The size method for an ArrayList returns the number of elements in the ArrayList .
This is different from the variable length for an array that returns the capacity of
the array. An ArrayList has no capacity. We can insert in it as many elements as we
want. The only restriction is the size of the available main memory.
7.9 Exercises
1. Write a method that takes in an ArrayList of doubles. The method should return the
second largest double in the ArrayList . You can assume that the ArrayList contains
at least two elements. The signature of the method should be as follows.
public static double secondLargest(ArrayList < Double > a)
Search WWH ::

Custom Search