Mistu4u
Mistu4u

Reputation: 5416

ArrayIndexOutOfBoundsException while printing a 3D array in JAVA

I wrote the following code in JAVA.

package threed;

import java.util.Scanner;

  public class Threed_Array {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
int b1,b2,b3;
System.out.print("Enter the number of elements in 1st bracket->");
Scanner sc=new Scanner(System.in);
b1=sc.nextInt();
System.out.print("Enter the number of elements in 2nd bracket->");

b2=sc.nextInt();
System.out.print("Enter the number of elements in 3rd bracket->");

b3=sc.nextInt();
int threedarray[][][]=new int[b1][b2][b3];

for(int i=1; i<=b1; i++)
{
    for(int j=1; i<=b2; j++)
    {
        for(int k=1; i<=b3; k++)
        {
          System.out.print("Enter element->");
          threedarray[i][j][k]=sc.nextInt();
        }
    }
}
for(int i=1; i<=b1; i++)
{
    for(int j=1; i<=b2; j++)
    {
        for(int k=1; i<=b3; k++)
        {
          System.out.print(" "+threedarray[i][j][k]);

        }
    }
}

}
}

I am getting ArrayIndexOutOfBoundsException for this code. This is showing in the line:

threedarray[i][j][k]=sc.nextInt();

Can anybody help me out where the error is occurring? Thank you.

Upvotes: 0

Views: 200

Answers (8)

nano_nano
nano_nano

Reputation: 12523

You should always start at index 0, it is the index of the first element of your array:

for(int i=0; i<b1; i++)
{
    for(int j=0; j<b2; j++)
    {
        for(int k=0; k<b3; k++) {
          System.out.print("Enter element->");
          threedarray[i][j][k]=sc.nextInt();
        }
    }
}
for(int i=0; i<b1; i++)
{
    for(int j=0; j<b2; j++)
    {
        for(int k=0; k<b3; k++)
        {
          System.out.print(" "+threedarray[i][j][k]);
        }
    }
}

furthermore make the check with < not <=

With the last loop you access the array element n+1 where n is the size of that array. Thats the reason for the exception.

Upvotes: 3

Thresh
Thresh

Reputation: 470

The problem here is that you are starting the loop from 1 till the b1,b2,b3 respectively. Array indexes start from 0 not from 1 and ends at the size of the array - 1. So to fix your code you need to modify the code to be the following:

for(int i = 0; i < b1; i++) {
  for(int j = 0; j < b2; j++) {
    for(int k = 0; k < b3; k++) {
      System.out.print("Enter element->");
      threedarray[i][j][k]=sc.nextInt();
    }
  }
}

More generally if you don't know the length/size of the array you are looping on, you can make the loop more generally to be less than the array length. So it would be i < threedarray.length and j < threedarray[i].length and k < threedarray[i][j].length.

The key idea is that the array indexing starts from 0 and ends at its size/length - 1, so to get the last element you access array[array.length-1] and to access the first element you access array[0]

Hope this answers your question.

Upvotes: 0

Laurent B
Laurent B

Reputation: 2220

Apart from the array index problem (see Stefan Beike's answer), you could you an ArrayList.

This would avoid having to ask the user for the number of elements you want to have in your matrix.

Still if you want to keep primitive arrays, you could use System.arrayCopy to reallocate to a greater size array.

Upvotes: 0

richardtz
richardtz

Reputation: 4993

I'd say the conditions in your loops are not correct :

for(int i=1; i<=b1; i++)
{
    for(int j=1; i<=b2; j++)
    {
        for(int k=1; i<=b3; k++)
        {

it should be :

for(int i=1; i<=b1; i++)
{
    for(int j=1; j<=b2; j++)
    {
        for(int k=1; k<=b3; k++)
        {

Also, you should start at 0 in each of them.

Upvotes: 2

Bucket
Bucket

Reputation: 7521

Indices start at 0, not 1. Start your three for loops at 0 and iterate to one less than the number provide:

for(int i = 0; i < b1; i++)
{
    for(int j = 0; i < b2; j++)
    {
        for(int k = 0; i < b3; k++)
        {
          System.out.print(" "+threedarray[i][j][k]);
        }
    }
}

Upvotes: 0

XpressOneUp
XpressOneUp

Reputation: 195

Arrays in Java are zero-based, try to iterate from 0 to b1-1:

for(int i=0; i<b1; i++)
{
    for(int j=0; i<b2; j++)
    {
        for(int k=0; i<b3; k++)
        {
          System.out.print("Enter element->");
          threedarray[i][j][k]=sc.nextInt();
        }
    }
}

Upvotes: 1

Hugo Sousa
Hugo Sousa

Reputation: 1926

I think you want j and k in the 2 inside for loops instead of i. Also, arrays in Java start in index 0, so it should look like this:

for(int i=0; i<b1; i++)
{
    for(int j=0; j<b2; j++)
    {
        for(int k=0; k<b3; k++)
        {  
           ...
        }
     }
}

Upvotes: 1

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

[b1][b2][b3]

You create an array using the inputs b1,b2,b3

The array created has length of bx but subscripts from 0 to bx-1. Hence you must loop from 0 to bx-1

Upvotes: 0

Related Questions