Results 1 to 3 of 3

Thread: [RESOLVED] A question about a java multidimensional array

  1. #1

    Thread Starter
    Addicted Member dim_kevin_as_human's Avatar
    Join Date
    Oct 2005
    Location
    Wisconsin
    Posts
    183

    Resolved [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.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3

    Thread Starter
    Addicted Member dim_kevin_as_human's Avatar
    Join Date
    Oct 2005
    Location
    Wisconsin
    Posts
    183

    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
  •  



Click Here to Expand Forum to Full Width