|
-
Jan 25th, 2010, 10:31 PM
#1
Thread Starter
Addicted Member
[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.
Dreaming men are haunted men.
-
Jan 25th, 2010, 10:39 PM
#2
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];
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jan 25th, 2010, 10:44 PM
#3
Thread Starter
Addicted Member
Re: A question about a java multidimensional array
ahhh i had to take the [][] off the variable...
thanks! i appreciate it!
Dreaming men are haunted men.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|