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..
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://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 }
it throws an arrayindexoutofboundsCode:sales = new double[people][products];
I assume this is because the variables are declared before anything actually gets passed into the constructor..but i could be wrong.




Reply With Quote
