-1

double brace initialization is good for have better visibility of the context of java code.

unfortunately StringBuilder can't use with double brace initialization like

    final String[] array = new String[] {"A", "B"};
    System.out.println(new StringBuilder(){{
                           for (String s : array){
                               append(s + "\n");
                           }
                       }}.toString());

is this a good alternative? any better suggestions? my intention for this question is not to find a way for string concatination. my intention is to find a way for use double brace with StringBuilder.

    final String[] array = new String[] {"A", "B"};
    System.out.println(new Object(){
        @Override public String toString(){
            StringBuilder stringBuilder = new StringBuilder();
            for (String s : array){
                stringBuilder.append(s + "\n");
            }
            return stringBuilder.toString();
        }
    });
Frank M.
  • 997
  • 1
  • 10
  • 19
  • output of strings is only my example - i like to find a possibility for using double brace initialization – Frank M. May 12 '14 at 08:22
  • StringBuilder is used to dynamically construct various strings, double brace init usually is for the exact opposite to construct static constants. Maybe your actual scenario is just not clear for me but what you are trying to do doesn't feel right. – gadget May 12 '14 at 09:05

3 Answers3

0

If what you are trying to achieve is just creating a string from an array of strings, you can use Arrays.toString() method:

System.out.println(Arrays.toString(array));

This method returns a string representation of the contents of the specified array.

Boris
  • 22,667
  • 16
  • 50
  • 71
  • output of strings is only my example - i like to find a possibility for using double brace initialization – Frank M. May 12 '14 at 08:21
0

with a extra utility class is double brace initialization possible. with

public static class StringBuild{
    private StringBuilder stringBuilder = new StringBuilder();

    public void append(String string){
        stringBuilder.append(string);
    }

    @Override
    public String toString(){
        return stringBuilder.toString();
    }
}

can i write

    final String[] array = new String[] {"A", "B"};
    System.out.println(new StringBuild(){{
            for (String s : array){
                append(s + "\n");
            }
        }
    });
Frank M.
  • 997
  • 1
  • 10
  • 19
0

Alternative method is not acceptable because it changed the class of StringBuilder object.

In your program first option is not working because StringBuilder is final. And you cannot change a final class. You can use the following code which will work in all situations.

    final String[] array = {"a", "b"};
    StringBuilder sb = new StringBuilder(new Object(){
        @Override
        public String toString() {
            String str = "";
            for(String st : array){
                str+=st;
            }
            return str;
        }
    }.toString());
    System.out.println(sb);
afzalex
  • 8,598
  • 2
  • 34
  • 61