stringBuilder对象的Java堆空间错误

时间:2017-08-30 15:45:23

标签: java string runtime-error heap-memory stringbuilder

class GfG
{

  static String encode(String str)
  {
    int count =1;
    char c ;
    StringBuilder result = new StringBuilder(200);
    int i=0;
    while(i<str.length()-1){
        int j =i+1;
        count =1;
        c = str.charAt(i);
        while(j<str.length() && str.charAt(j)==c  ){
            count++;
            j++;
        }
        result.append(c+""+count);
        if(str.charAt(str.length()-1)!=c && j==str.length()-1)
            i=j-1;
        else
            i=j;
    }
    return result.toString();
}

public static void main(String[] args) {
    String res = encode("hnwnkuewhsqmgbbuqcljjivswmdkqtbxixmvtrrbljptnsnfwzqfjmafadrrwsofsbcnuvqhffbsaqxwpqcac");
    if(res.equals("h1n1w1n1k1u1e1w1h1s1q1m1g1b2u1q1c1l1j2i1v1s1w1m1d1k1q1t1b1x1i1x1m1v1t1r2b1l1j1p1t1n1s1n1f1w1z1q1f1j1m1a1f1a1d1r2w1s1o1f1s1b1c1n1u1v1q1h1f2b1s1a1q1x1w1p1q1c1a1c1"))
        System.out.println("yes");
    else
        System.out.println("no");
   }
}

我收到此错误,我不知道如何处理它。谁能告诉我这个错误的原因和解决方案。

线程中的异常&#34; main&#34; java.lang.OutOfMemoryError:Java堆空间     at java.util.Arrays.copyOf(Arrays.java:3332)     at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)     在java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)     在java.lang.StringBuilder.append(StringBuilder.java:136)     在GfG.encode(GFG.java:18)     在GfG.main(GFG.java:28)

0 个答案:

没有答案