StringBuilder Flashcards

1
Q

The method that changes the length of a StringBuilder object…

A

public void setLength(int newLength)
Sets the length of the character sequence.

 public class TestClass{    
public static void main(String args[ ] ){       
  StringBuilder sb = new StringBuilder("12345678");       
  sb.setLength(5);       
  sb.setLength(10);       
  System.out.println(sb.length());   // will print 10
  } 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

substring methods..

A

public String substring(int begin Index)

public String substring(int start, int end)

String s = "blooper";     
StringBuilder sb = new StringBuilder(s);     
sb.append( s.substring(4)).delete(3, 5);     System.out.println(sb); // out bloerper
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

StringBuilder has 4 constructors ..

A

There are four ways to construct a StringBuilder:

new StringBuilder() - Constructs a string builder with no characters in it and an initial capacity of 16 characters.

StringBuilder(CharSequence seq) - Constructs a string builder that contains the same characters as the specified CharSequence.

StringBuilder(int capacity) - Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.

StringBuilder(String str) - Constructs a string builder initialized to the contents of the specified string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Same methods in StringBuilder as in String…

A

charAt(), indexOf(), length(), and substring()

charAt(int index)
Returns the char value in this sequence at the specified index.

length()
Returns the length (character count).

indexOf(String str, int from Index)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

insert() method

A

The insert() method adds characters to the StringBuilder at the requested index and returns a reference to the current StringBuilder.

insert(int offset, String str)
insert(int offset, (almost all data types))
Inserts the string into this character sequence.but something to see after

Offset behaves exactly as an index in this case, however it’s different…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Offset

A

In computer science, offset describes the location of a piece of data compared to another location. For example, when a program is accessing an array of bytes, the fifth byte is offset from the beginning of the array by four bytes. If the array contained data that is meant to be read 32 bits at a time, element n would be offset from the start of the array by 4 * (n - 1) bytes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

delete methods..

A

delete() and deleteCharAt()

StringBuilder delete(int start, int end) 
StringBuilder deleteCharAt(int index)

It removes characters from the sequence and returns a reference to the current StringBuilder.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

reverse a string in StringBuilder

A

reverse()

Reverses the chars of the string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

equality with StringBuilder..

A
StringBuilder one = new StringBuilder(); 
StringBuilder two = new StringBuilder(); 
StringBuilder three = one.append("a"); System.out.println(one == two); // false System.out.println(one == three); // true , both references point to the same object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

StringBuilder methods inherited from Object…

A

clone, equals, finalize, getClass, hashCode, notify, ….

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

StringBuilder methods for the exam..

A
charAt(), 
indexOf(), 
length(), 
substring()
append()
insert()
delete()
deleteCharAt()
reverse()
toString()

public void ensureCapacity(int minimumCapacity)
public int capacity()
public void setLength(int newLength)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Method that returns the current capacity.

A

public int capacity()
Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Method ensures that the capacity is at least equal to the specified minimum.

A

public void ensureCapacity(int minimumCapacity)

Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

public void setLength(int newLength)

Sets the length of the character sequence

A

public void setLength(int newLength)

Sets the length of the character sequence.
If the parameter is greater than the length of the string, empty spaces are added to the string. str.length() will return the number of chars passed as newLength on the method parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly