Forums Register Login

Arrays

+Pie Number of slices to send: Send
Iam creating an array in a class which has no main method like this:
int[][] Allocation = new int[n][3];
Then Iam trying to initialise all the elements to 1 in the constructor:
n is passed in through the constructor(parameter)
for (int row=0; row <= n-1; row++) {
for (int col=0; col <=2; col++) {
Allocation[row][col] = 1; } }
It gives me an error:
java.lang.ArrayIndexOutOfBoundsException: 0

Please Help me out!!!
+Pie Number of slices to send: Send
My guess (there are other possibilities, but this one is likely) is that you've got something like this:

Here the array will be constructed when "n" is still 0, because instance initializers run before the constructor body.
To fix this, move the array construction inside the constructor -- i.e.,
+Pie Number of slices to send: Send
Thanks a lot!!! It Works
Iam working on a program that uses many arrays like that.And ive been creating the whole array inside.Using the new keyword inside the constructor was the problem I wasnt able to detect.
I really appreciate it and Thanks once again!!!
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 705 times.
Similar Threads
Arrays with files
Could you please help me?
help on printing letter X using *
2D Array Trouble
Printing multi-dimesonial arrays
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 01:26:08.