Jump to content

How to fix Array Index out of Bounds in two-dimensinal arrays in Java using loops?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Nokturnal

Nokturnal

    Newbie

  • Members
  • Pip
  • 4 posts
I know this is probably a noob question but this is just making me mad. I don't understand why this won't work.

I'm trying to create a 2 dimensional array (acting as a matrix) and then fill it with either a for or while loop.

Essentially, I'm saying:

int [][] matrix = new int [1][1];

In my mind, this is creating 2 arrays with 2 indexes. I believe this is correct, but hey, I've been wrong before, and I'm no pro at coding.

Now, my nested for loops weren't working (index out of bounds) so I'm simplifying it by using a while loop. When I say simplify, I mean that I'm only trying to fill half of each array.

So I declared:

int row = 0;

int col = 0;

then:

while (row < 2) {


System.out.println("Enter x");

matrix [row][col] = reader.nextInt();

row++;


}

Okay so in my mind, this SHOULD be able to fill the 1st index of BOTH arrays. However, I get the dreaded array index out of bounds error. I don't understand why. Isn't it essentially saying:

1st loop, matrix [0][0] = 1xval
2nd loop, matrix [1][0] = 2xval

So here's what happens when I run it:

Enter x
1

Enter x
arrayindexoutofbounds

Maybe I'm making a fool of myself by missing something really easy, but hey, this is a good way to learn. Arrays ALWAYS seem to do this to me. Could someone help me out?

Edited by Alyn, 16 October 2011 - 07:15 PM.
added code tag


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
The 2 dimensional array you've created is a 1x1 array.
So your array looks like:

array = new int[1][1];

0

a 2x1 array looks like:

array = new int[2][1];

0

0

// a 2x2 array

array = new int[2][2];

0 0

0 0

// a 1x2 array

array = new int[1][2];

0 0


Another way to look at it is
 

array = new int[[COLOR="#FF8C00"]numberOfRows[/COLOR]][[COLOR="#FF8C00"]numberOfColumns[/COLOR]];


Also a simple way to loop through each position of your array:

for(int row = 0; row < array.length; ++row) {

    for(int column = 0; column < array[row].length; ++column){

        array[row][column] = someValue;

    }

}

Note* When you first initialize an int array, all the values are set to 0.

Example:

numberOfRows = 4;

numberOfColumns = 4;

array = new int[numberOfRows][numberOfColumns];


for(int row = 0; row < array.length; ++row) {

    for(int col = 0; col < array[row].length; ++col) {

        array[col][row] = 2;

    }

}


[COLOR="#008000"]// the array looks like this initially:[/COLOR]

0 0 0 0

0 0 0 0

0 0 0 0

0 0 0 0

[COLOR="#008000"]// after 1st iteration[/COLOR]

2 0 0 0

0 0 0 0

0 0 0 0

0 0 0 0

[COLOR="#008000"]// 2nd iteration[/COLOR]

2 2 0 0

0 0 0 0

0 0 0 0

0 0 0 0

[COLOR="#008000"]// 3rd iteration[/COLOR]

2 2 2 0

0 0 0 0

0 0 0 0

0 0 0 0

[COLOR="#008000"]// 4th [/COLOR]

2 2 2 2

0 0 0 0

0 0 0 0

0 0 0 0

[COLOR="#008000"]// 5th iteration[/COLOR]

2 2 2 2

2 0 0 0

0 0 0 0

0 0 0 0

[COLOR="#008000"]// .. and so on[/COLOR]

2 2 2 2 

2 2 2 2

2 2 2 2

2 2 2 2



#3
Nokturnal

Nokturnal

    Newbie

  • Members
  • Pip
  • 4 posts
@lethal

Thank you very much!

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Also note that from the moment you create arrays with more than 1 dimension, you don't need to specify the size of the last one(s).
int[][] intArray = new int[3][];
Cause if it was obliged, you could only make rectangle matrixes (single height & width)
By this you can specify different sizes per row
int[][] intArray = new int[3][];
intArray[0] = new int[2];
intArray[1] = new int[3];
intArray[2] = new int[1];

Array:
0  0
0  0  0
0






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users