Mohamed Mamdoh
Mohamed Mamdoh

Reputation: 11

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 20, Size: 20

I'm trying to write a lz78 code as a school project, but I keep getting this error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 20, Size: 20

Here is the code:

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);
    }

}

Upvotes: 0

Views: 826

Answers (1)

Andrei
Andrei

Reputation: 5637

Replace for(int i=0;i<=input.size(); i++) with for(int i=0;i<input.size(); i++)

Upvotes: 3

Related Questions