线程“main”中的异常java.lang.IndexOutOfBoundsException:索引:20,大小:20

时间:2018-05-19 20:51:34

标签: java

我正在尝试将lz78代码写为学校项目,但我不断收到此错误:

  

线程“main”中的异常java.lang.IndexOutOfBoundsException:Index:   20,尺寸:20

以下是代码:

public class LZ88 {
    ArrayList input = new ArrayList();
    ArrayList stored = new ArrayList();
    Iterator counter = input.iterator();
    int count=0;
    int lz78(String x)
    {
        int length=x.length();
        for(int i=0;i<x.length();i++)
        {
            input.add(x.charAt(i));
        }
        for(int i=0;i<=input.size(); i++)
        {
            if(stored.contains(input.get(i))==true)
            {
                String str ;
                StringBuilder sb = new StringBuilder();
                sb.append(input.get(i));
                sb.append(input.get(++i));
                str=sb.toString();
                while(stored.contains(str)==true)
                {
                    sb.append(input.get(++i));
                    str=sb.toString();

                }
                stored.add(str);
                System.out.println(stored);
            }
            else
            {
                stored.add(x.charAt(i));
                System.out.println(stored);
            }
        }
        return 0;
    }
    public static void main(String[] args) {
        String x ="abaababaababbbbbbbba";
        LZ88 ob = new LZ88();
        ob.lz78(x);
    }

}

1 个答案:

答案 0 :(得分:3)

for(int i=0;i<=input.size(); i++)替换为for(int i=0;i<input.size(); i++)