1

The following is a part of my code. For some values of bands and bandRows the code seems to run perfectly alright. But for some it gives an ArrayIndexOutOfBounds exception. Any ideas where I might have gone wrong? I cannot find any mistake in the code. Thanks in advance

for(int i=0; i<bands; i++)
        {
            int a=0;
            while(a<bucketSize) 
            { 
                bandBuckets[i][a] = new ArrayList();
                a++;
            }
        }  

    for (int i = 0; i < bands; i++) 
            {          
                for (int j = 0; j < preprocessedList.size(); j++) 
                {
                    int[][] forBuckets = new int[bands][bandRows];

                    for (int k = 0; k < bandRows; k++) 
                    {
                        Arrays.fill(forBuckets[i], Bands[i][k][j]);

                    }

                    bandBuckets[i][h.hashBands(forBuckets[i], bucketSize)].add(j);

                }
            }   

Here's the h.hashBands() function which is in another class

 public int hashBands(int[] in, int bucketSize) 
        {

            int hashVal = 0;
            int k = in.length;
            int base = 3;
            for (int i = 0; i < in.length; i++) {
               // for (int j = 0; i < in[i].length; i++) 
                    hashVal += in[i] * Math.pow(base, k - i - 1);
            }
            return hashVal % bucketSize;

        }
4
  • Too many arrays, too many dimensions... What is Bands[i][k][j] .... are those dimensions right .... Commented Nov 20, 2013 at 4:11
  • yeah they are right. It works perfectly well for some inputs. Only for some, it gives the error Commented Nov 20, 2013 at 4:13
  • IMO, this should be in Code Review. Commented Nov 20, 2013 at 4:37
  • @ChthonicProject - CR is for working code only. Commented Nov 21, 2013 at 3:39

2 Answers 2

1

Perhaps there is an overflow in your hashBands() function.

The max value for an int is 231 - 1. hashVal will overflow when k - i - 1 is greater than 19. In Java, exceptions aren't thrown for overflows and underflows. Consider using a BigInteger and its modPow() function.

Sign up to request clarification or add additional context in comments.

Comments

0

Can you tell where exactly you are getting ArrayIndexOutOfBoundException. Seeing your code it seems that there might be some problem while returning from hashBands() function. It might be returning something greater than expected.

bandBuckets[i][h.hashBands(forBuckets[i], bucketSize)]

For the 2nd dimension of this array h.hashBands(forBuckets[i], bucketSize) --> this value might be greater than the expected value for that part.....

1 Comment

Yes it's that line bandBuckets[i][h.hashBands(forBuckets[i], bucketSize)].add(j); But that value should be less than bucketSize. The function hashBands() return the mod of hash value by bucketSize (hashVal % bucketSize). So obviously it is less than the bucketSize..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.