I've been wondering about the implementation of charAt function for String/StringBuilder/StringBuffer in java what is the complexity of that ? also what about the deleteCharAt() in StringBuffer/StringBuilder ?
The built-in Java string charAt() method returns a character at a particular index position in a string. The first character has the index value 0 and so on for subsequent characters in the string. charAt() accepts one parameter: the index position of the character you want to retrieve.
The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove. Remove last character of a string using sb. deleteCharAt(str.
Therefore the time complexity of charAt should be int O(1).
In Java (maybe the same for other langues as well) the charAt(x) method for accessing characters in a string is much slower than accessing the character elements in a char[] .
For String
, StringBuffer
, and StringBuilder
, charAt()
is a constant-time operation.
For StringBuffer
and StringBuilder
, deleteCharAt()
is a linear-time operation.
StringBuffer
and StringBuilder
have very similar performance characteristics. The primary difference is that the former is synchronized
(so is thread-safe) while the latter is not.
Let us just look at the corresponding actual java implementation(only relevant code) for each of these methods in turn. That itself will answer about their efficiency.
public char charAt(int index) { if ((index < 0) || (index >= value.length)) { throw new StringIndexOutOfBoundsException(index); } return value[index]; }
As we can see, it is just a single array access which is a constant time operation.
public synchronized char charAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); return value[index]; }
Again, single array access, so a constant time operation.
public char charAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); return value[index]; }
Again, single array access, so a constant time operation. Even though all these three methods look same, there are some minor differences. For example, only StringBuffer.charAt method is synchronized but not other methods. Similarly if check is slightly different for String.charAt (guess why?). Closer look at these method implementations itself give us other minor differences among them.
Now, let us look at deleteCharAt implementations.
String does not have deleteCharAt method. The reason might be it is an immutable object. So exposing an API which explicitly indicates that this method modifies the object is not probably a good idea.
Both StringBuffer and StringBuilder are subclasses of AbstractStringBuilder. The deleteCharAt method of these two classes is delegating the implementation to its parent class itself.
public synchronized StringBuffer deleteCharAt(int index) { super.deleteCharAt(index); return this; }
public StringBuilder deleteCharAt(int index) { super.deleteCharAt(index); return this; }
public AbstractStringBuilder deleteCharAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); System.arraycopy(value, index+1, value, index, count-index-1); count--; return this; }
A closer look at AbstractStringBuilder.deleteCharAt method reveals that it is actually calling System.arraycopy. This can be O(N) in worst case. So deleteChatAt method is O(N) time complexity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With