[RESOLVED] A question about a java multidimensional array
During the design of my java class, I need to use an 2d array to keep track of products and salespersons. I would like to declare the private variable at the top, and then create it in my constructor with data that gets passed into the constructor at run-time...it would see that any way i try to word this it doesn't allow it..
Code:
//declare variables
private int _people;
private int _products;
private double sales[][];
//constructor
public Sales( int people , int products )
{
//double sales[][] = new double [ people ] [ products ]; //this tells me "local variable hides field"
//sales[ 0 ] = new double [ products ];
sales[][] = new double [ people ] [ ]; //does not work, says people is already defined
}
so my question is, how do I declare the var above, and then fill it in later. i have to follow the UML given to me so it has to work this way. If I try to do this:
Code:
sales = new double[people][products];
it throws an arrayindexoutofbounds
I assume this is because the variables are declared before anything actually gets passed into the constructor..but i could be wrong.
Re: A question about a java multidimensional array
try
Code:
sales = new double[people][];
for (int i = 0; i < sales.length; i++)
sales[i] = new double[products];
Re: A question about a java multidimensional array
ahhh i had to take the [][] off the variable...
thanks! i appreciate it!