Slow string concatenation over large input

前端 未结 6 778
醉酒成梦
醉酒成梦 2021-01-12 13:47

I\'ve written an n-ary tree ADT which works fine. However, I need to store its serialization in a variable a calling class. eg.

    DomTree a         


        
6条回答
  •  天涯浪人
    2021-01-12 14:15

    Don't use string concatenation in loops. It does not scale.

    Use StringBuilder, this does not make new objects all the time, like string concatenation..

    void print() {
    StringBuilder sb = new StringBuilder();
    sb.append("hello");
    sb.append(" World!");
    System.out.println(sb.toString());
    

    }

提交回复
热议问题