3 StringBuilder Flashcards
What is the default length of a StringBuilder object?
Length is different than capacity. The lenght is equal to the string argument passed. If no argument is passed, length =0. StringBuilder has length NOT size.
Does StringBuilder object have a size?
No, it has length and capacity.
What is the default capacity for a StringBuilder object and the constructor?
Default capacity is 16 when no specific capacity is passed to the constructor. String Builder sb = new StringBuilder(); // makes a sb object with capacity 16
How do you write a constructor for a StringBuilder with a set capacity?
String Builder sb = new StringBuilder(5); //capacity set to 5
How do you convert a StringBuilder to a String?
String s=sb.toString();
What are the 10 methods for the StringBuilder class?
acddiilrst
append(), charAt(), delete(), deleteCharAt(), indexOf(), insert(), length(), reverse(), substring(), and toString()
T or F. A sb object is changed when a method performs an operation. An assignment is NOT required.
True, for most of the methods. subString & toString do not.
sb.append(‘x’) is the same as sb=sb.append(x)
What index does StringBuilder start at?
Zero! sb.charAt(0) is the first letter.
Which is a real StringBuilder method used to create the capacity of the sb object?
setCapacity(int num);
ensureCapacity(int num);
ensureCapacity(int num);
- there is no setCapacity method.
StringBuilder sb = new StringBuilder(); sb.ensureCapacity(100);