Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone() StringBuilder

Is there some ways how I could clone StringBuilder ? I am reading files by bits then convert these bits to ASCII chars after that I collect chars into String builder and when I have for example 8 chars I put that String Builder object into Array List. Then I clean it and again do the same. However I can't create new string builder because of memory and I can't do changes to that String builder because in Array List also that builder changes.

So I think I have to clone that String Builder and put it into Array List. There is just one problem String Builder don't have clone(). So what is my alternatives ?

Maybe someone could give some ideas what is neat way to do this considering about performance and memory.

ArrayList characters = new ArrayList(); int counter = 0;

StringBuilder sb = new StringBuilder(blockSize-1);

while (mbb.hasRemaining()) {   

char charAscii = (char)mbb.get();


    counter++;
    charCounter++;

     if (counter == blockSize){

        sb.append(charAscii);
        characters.add(sb);//sb.toString()
        sb.delete(0, sb.length());
        counter = 0;

    }else{

        sb.append(charAscii);

     }

 if(!mbb.hasRemaining()){
    characters.add(sb);
}



}
fc.close();
return characters;
like image 937
Streetboy Avatar asked Mar 01 '12 19:03

Streetboy


People also ask

Can you iterate through a StringBuilder?

A for-loop can iterate over the characters in a StringBuilder. We access the length() method to get the StringBuilder's size and then use charAt() to access chars. Length This method returns the count of characters in the StringBuilder.

Can we convert StringBuilder to string in Java?

To convert a StringBuilder to String value simple invoke the toString() method on it. Instantiate the StringBuilder class. Append data to it using the append() method. Convert the StringBuilder to string using the toString() method.

What does StringBuilder setLength do?

The setLength(int newLength) method of StringBuilder is used to set the length of the character sequence equal to newLength. For every index k greater then 0 and less than newLength. If the newLength passed as argument is less than the old length, the old length is changed to the newLength.

How do you access elements in StringBuilder?

charAt() method returns the char value in this sequence at the specified index. The first char value is at index 0, the next at index 1, and so on, as in array indexing. The index argument must be greater than or equal to 0, and less than the length of this sequence.


2 Answers

If you don't have the memory to create a new StringBuilder, then you don't have the memory to create a new StringBuilder, and cloning wouldn't change that. The only real way to copy a StringBuilder is new StringBuilder(myBuilder), or something equivalent.

If you're getting OutOfMemoryException, you'll need to either get more memory, or find some other way to reduce memory consumption.

like image 108
Louis Wasserman Avatar answered Sep 28 '22 20:09

Louis Wasserman


You can clear StringBuilder object by:

1.

sb.delete(0, sb.length()) 

2.

sb = null;
sb = new StringBuilder();

For performance, 1. is the better option. But you cannot clone it anyway. For more info about clone see

  • Shallow copy and Deep Copy
  • Clone
like image 41
Tapas Bose Avatar answered Sep 30 '22 20:09

Tapas Bose