patternjavaModerate
A custom String Builder implementation
Viewed 0 times
customimplementationstringbuilder
Problem
Two code files below : the actual logic & the test logic.
Core Logic
Test Class
```
package stringBuilder;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
NoksStringBuilder stringBuilder = new NoksStringBuilder();
Scanner inputScanner = new Scanner(System.in);
int i = 0;
while(i != 3){
printMenu();
i = Integer.parseInt(inputScanner.nextLine());
if(i == 1){
System.out.println("Enter string");
stringBuilder.add(inputScanner.nextLine());
}
else if( i == 2){
System.out.println(stringBuilder.toString());
}
}
}
private static void printMenu() {
System.out.println("StringBuilder Options :");
System.out.println("1.Add");
System.
Core Logic
package stringBuilder;
public class NoksStringBuilder {
//initial capacity
private static final int INITIAL_SIZE = 3;
String[] stringList = new String[INITIAL_SIZE];
//number of strings in the builder currently
int size = 0;
//total number of chars in the builder , sum total of length of each string
int characterCount = 0;
public void add(String s){
if(size < stringList.length){
stringList[size++] = s;
characterCount += s.length();
}
else{
String[] temp = new String[stringList.length*2];
for(int i =0 ; i< stringList.length; i++){
temp[i] = stringList[i];
}
stringList = temp;
add(s);
}
}
public String toString(){
char[] output = new char[characterCount];
int outputIndex = 0;
for(int i = 0; i < size; i++){
for(int j = 0; j < stringList[i].length(); j++){
output[outputIndex++] = stringList[i].charAt(j);
}
}
return new String(output);
}
}Test Class
```
package stringBuilder;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
NoksStringBuilder stringBuilder = new NoksStringBuilder();
Scanner inputScanner = new Scanner(System.in);
int i = 0;
while(i != 3){
printMenu();
i = Integer.parseInt(inputScanner.nextLine());
if(i == 1){
System.out.println("Enter string");
stringBuilder.add(inputScanner.nextLine());
}
else if( i == 2){
System.out.println(stringBuilder.toString());
}
}
}
private static void printMenu() {
System.out.println("StringBuilder Options :");
System.out.println("1.Add");
System.
Solution
public class NoksStringBuilder {When told that something implements
StringBuilder, I would expect it to implement the same interfaces. public class NoksStringBuilder implements CharSequence, Appendable, Serializable {This way it can be used as a replacement for a
StringBuilder used in more generic circumstances. String[] stringList = new String[INITIAL_SIZE];You call this a
stringList, but it's actually a String array. If it were an actual List, you could quit mucking around managing capacity. List strings = new ArrayList<>();This would push all the memory management off onto the
List. But I actually don't think that anything involving String is correct here. More on that later.public void add(String s){Similarly, I would call this
public Appendable append(CharSequence csq) {Then you could use it the same way as with a real
StringBuilder. Or you could use it the way you do your method (realizing that a String is a CharSequence). All this would make obvious that an array of
String is not a good way to hold a StringBuilder. For one thing, while it makes for a quick append of a String, it works less well with letters and you lose the random access aspect. I'd probably try private char[] sequence = new char[DEFAULT_CAPACITY];That puts more work into
add but makes toString simpler. public String toString() {
return new String(sequence, 0, length);
}Note that
length is an object field that you'll have to maintain. It's the equivalent of your characterCount. Note that if you want to implement
capacity(), you could just say public int capacity() {
return sequence.length;
}Code Snippets
public class NoksStringBuilder {public class NoksStringBuilder implements CharSequence, Appendable, Serializable {String[] stringList = new String[INITIAL_SIZE];List<String> strings = new ArrayList<>();public void add(String s){Context
StackExchange Code Review Q#133036, answer score: 12
Revisions (0)
No revisions yet.