user1843145
user1843145

Reputation: 35

arraylist size exception

for(int i=0;i<charset.size();i++)
{
    for(int j=0;i<charset.size();j++)
    {
        for(int k=0;k<charset.size();k++)
        {
            String plaintext= charset.get(i)+charset.get(j)+charset.get(k);


            String hashtext=CreateHash.returnString(plaintext);

            BufferedWriter bw = new BufferedWriter(new FileWriter("/root/MD5List.txt", true)); //opens file
            bw.write(plaintext+" = "+hashtext);
            bw.newLine();
            bw.close();
        }
    }
}

arraylist size is 10. When I run this program, I get a stacktrace:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 10 at java.util.ArrayList.rangeCheck(ArrayList.java:571) at java.util.ArrayList.get(ArrayList.java:349) at MD5HashTable.Hash.main(Hash.java:57) Java Result: 1

Since the variable k is clearly less than the size of the arraylist, why do I get this error?

Upvotes: 0

Views: 724

Answers (2)

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

Try to use 


  for(int i=0;i<charset.size();i++)
       {
        for(int j=0;j<charset.size();j++)
        {

Upvotes: 0

Looks like you have a typo. In the j loop, you're testing i:

i<charset.size();

so j continues to increment. You want to test j:

j<charset.size();

Upvotes: 9

Related Questions