-1

I am trying to create a 2D array of squares (custom class) for a live wallpaper. I calculate how many can be displayed using the screen width and height and final ints for the square side length and gap distance between them. When initializing the squares in the array I use nested for loops. However, when I run this on an emulator I get an index out of bounds.

java.lang.ArrayIndexOutOfBoundsException: length=6; index=6

Why would the for loop not work?

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
    this.width = width;
    this.height = height;

    calcSquares();
    initSquares();

    super.onSurfaceChanged(holder, format, width, height);
}

private void calcSquares()
{
    numSquaresW = width/SQUARE_SIDE;
    numSquaresH = height/SQUARE_SIDE;

    while(width % (numSquaresW * SQUARE_SIDE) < (numSquaresW + 1) * SQUARE_GAP)
            numSquaresW--;
    while(height % (numSquaresH * SQUARE_SIDE) < (numSquaresH + 1) * SQUARE_GAP)
            numSquaresH--;

    marginW = ((width % numSquaresW) - ((numSquaresW - 1) * SQUARE_GAP)) / 2;
    marginH = ((height % numSquaresH) - ((numSquaresH - 1) * SQUARE_GAP)) / 2;

    squares = new WallpaperSquare[numSquaresW][numSquaresH];
}

private void initSquares()
{
    synchronized(squares)
    {
        for(int i=0; i<numSquaresW; i++)
        {
            for(int j=0; j<numSquaresH; i++)
            {
                int color = Color.HSVToColor(new float[] { (float) (360.0 * Math.random()), 1.0f, 1.0f });
                int xLoc = marginW + (SQUARE_SIDE + SQUARE_GAP) * i;
                int yLoc = marginH + (SQUARE_SIDE + SQUARE_GAP) * j;

                squares[i][j] = new WallpaperSquare(xLoc, yLoc, color);
            }
        }
    }
}
  • Learning to use your debugger (you use eclipse?) would save you a lot of frustration later on. Just saying. – rolfl Nov 15 '13 at 05:44

4 Answers4

5

Inside initSquares, you have for(int j=0; j<numSquaresH; i++). The last clause of the for should be j++.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
1

In the second loop you should have

 for(int j=0; j<numSquaresH; j++)  // <-- note the 'j++' and not the 'i++'
        {
            int color = Color.HSVToColor(new float[] { (float) (360.0 * Math.random()), 1.0f, 1.0f });
            int xLoc = marginW + (SQUARE_SIDE + SQUARE_GAP) * i;
            int yLoc = marginH + (SQUARE_SIDE + SQUARE_GAP) * j;

            squares[i][j] = new WallpaperSquare(xLoc, yLoc, color);
        }

in place of

 for(int j=0; j<numSquaresH; i++)
        {
            int color = Color.HSVToColor(new float[] { (float) (360.0 * Math.random()), 1.0f, 1.0f });
            int xLoc = marginW + (SQUARE_SIDE + SQUARE_GAP) * i;
            int yLoc = marginH + (SQUARE_SIDE + SQUARE_GAP) * j;

            squares[i][j] = new WallpaperSquare(xLoc, yLoc, color);
        }
rolfl
  • 17,539
  • 7
  • 42
  • 76
Apoorv
  • 13,470
  • 4
  • 27
  • 33
0

write j++ instead of i++ in second loop . . . . .

kamal vaid
  • 862
  • 7
  • 12
0

In the Inner for loop try to place j++

  for(int j=0; j<numSquaresH; j++)
vinay Maneti
  • 1,447
  • 1
  • 23
  • 31