0

I am new to Java and i am using eclipse for its compilation. I have seen many forums but i am not able to get around this error. I am creating a program for my homework and this is a small section of that program which is giving weird error. Any help is appreciated.

Here is where i am getting error -> aTwoD[i][j] = 0; <- at Initialize2D.<init>(Initialize2D.java:19)

I am stuck on this for quite some time now. :-(

What is did ->

public class Initialize2D
{
    private int[][] aTwoD;

    public Initialize2D (int N)
    {   
        System.out.println("N = " +N);
        int counter = 0;
        aTwoD = new int[N][N];

        int i = 1;
        while( i <= N ) 
        {
            int j = 1;
            while( j <= N )
            {
                System.out.println("counter = " +counter);
                aTwoD[i][j] = 0;
                System.out.println("aTwoD["+i+"]["+j+"] = " + aTwoD[i][j]);
                j++;
                counter++;
            }
            i++;
        }  
    }

    public static void main( String[] args)
    {
        Initialize2D TwoDArray = new Initialize2D(2);
    }   
}   
1
  • error i was getting -> N = 2 counter = 0 aTwoD[1][1] = 0 counter = 1 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Initialize2D.<init>(Initialize2D.java:19) at Initialize2D.main(Initialize2D.java:30) Commented Feb 9, 2014 at 9:44

4 Answers 4

6

index starts from 0 so <= would cause out of bound

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

6 Comments

thanks Jigar. This really makes me feel stupid. But what i was trying to do was create an 2D array from aTwoD[1][1] to aTwoD[2][2]. That is why i was using "<=" and N = 2. Is there a special way of doing that ?
if you specifically want to access [2][2] then you need array to contain 3 elements in both dimensions, change argument so that it creates [3][3]
Got it JB Nizet. I am very new to programming and i didn't know that.
Thank you to all of you guys for helping out. I really appreciate it.
Finally, if anyone felt bad that i copied by subsequent question in everyone's response, i again ask for forgiveness. I didn't mean to hurt anyone. Thanks for telling me this thing.
|
1

Array indices in Java start at 0, and end at length - 1. They don't start at 1 and end at length as your code assumes.

1 Comment

thanks Jigar. This really makes me feel stupid. But what i was trying to do was create an 2D array from aTwoD[1][1] to aTwoD[2][2]. That is why i was using "<=" and N = 2. Is there a special way of doing that ?
1

change

while( j <= N )

to

while( j < N )

In java indexing of N size array goes from 0 to N-1 including.

2 Comments

thanks Jigar. This really makes me feel stupid. But what i was trying to do was create an 2D array from aTwoD[1][1] to aTwoD[2][2]. That is why i was using "<=" and N = 2. Is there a special way of doing that ?
@user3043882, if your 2d array has only one element, you declare it aTwoD[1][1] and access as aTwoD[0][0] (it is unique element).
0

Beware that you iterate over i and j with the condition i <= N, j <= N.

Arrays in Java are zero based, meaning that they range between: 0 ... N-1. If you access them with N that will be out of their range.

Change your iterator from i <= N to i < N (same for j). That should do the trick.

1 Comment

thanks Jigar. This really makes me feel stupid. But what i was trying to do was create an 2D array from aTwoD[1][1] to aTwoD[2][2]. That is why i was using "<=" and N = 2. Is there a special way of doing that ?

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.