Brothah Heffay
Brothah Heffay

Reputation: 25

How to pass a multidimensional array into constructors?

I'm declate the multidimensional array propertyArray = new int[numbHuman][40]

where [numbHuman] is the number of rows, and the [40] is the number of columns (or vice versa doesnt really matter). Anyway I create those propertyArray's in a nested for loop, but I'm not sure How I'd pass propertyArray[][] into the player object constructor. Here is my code, I will try to clarify if needed.

import java.util.Scanner;
import java.util.Random;
public class PlayerArray
{
    Scanner scan = new Scanner(System.in);
    private int numbHuman;
    private Player[] arr;
    private String[] userName;
    //private 
    private int[] testArray;
    private int[][] propertyArray;
    private int[] userID;
    private int startingMoney;
    private int startingPosition;
    private int b;
    private int c;
    private int i;
    //private int d;
    public PlayerArray()
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("How many players wish to play? Values of 2 through 8 accepted.");
        numbHuman = scan.nextInt();
        System.out.println(numbHuman + " players selected.");
        while (numbHuman < 2 || numbHuman > 8)
        {
            System.out.println("Invalid entry, try again.");
            numbHuman = scan.nextInt();
        }       
        arr = new Player[numbHuman];
        i=0;
        testArray = new int[40];
        propertyArray = new int[numbHuman][40];///// WORK ON DIS 
        userName = new String[numbHuman];
        userID = new int[numbHuman];
        startingMoney = 1500;
        startingPosition = 0;
        b=0;
        c=0;
        for(int i = 0; i < arr.length; i++)
        {

            userID[i] = b;
            System.out.println("Player " + (i + 1) + ", Please enter your first name:");
            userName[i] = scan.next();

            for(c = 0; c < 40; c++)
            { 
                propertyArray[i][c] = 0;
            }

            arr[i] = new Player(userName[i],startingMoney,userID[i],startingPosition,propertyArray[i][c]);
            b++;
        }
    }

I'm trying to pass it into, how do i go about this? All the other variables work but the multidimensional array.

public Player(String userName, int changeInMoney, int userID, int startingPosition, int[][]propertyArray)
    {
        myID = userID;
        myName = userName;
        currentPosition += startingPosition;
        currentBal -= payBank(changeInMoney);
    }

Upvotes: 0

Views: 1746

Answers (2)

musical_coder
musical_coder

Reputation: 3896

Just add an extra pair of brackets:

public Player(String userName, int changeInMoney, int userID, int startingPosition, int[][] propertyArray)

Upvotes: 0

Mark Elliot
Mark Elliot

Reputation: 77074

You need to specify a set of square brackets for each dimension, e.g.:

public Player(String userName, int changeInMoney, int userID, 
    int startingPosition, int[][] propertyArray)

Upvotes: 1

Related Questions