如何将多维数组传递给构造函数?

时间:2013-12-03 01:30:41

标签: java arrays object multidimensional-array parameter-passing

我正在计算多维数组propertyArray = new int[numbHuman][40]

其中[numbHuman]是行数,[40]是列数(反之亦然)。无论如何,我在嵌套的for循环中创建了那些propertyArray,但我不确定如何将propertyArray [] []传递给播放器对象构造函数。这是我的代码,如果需要,我会尽力澄清。

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++;
        }
    }

我正试图将其传入,我该如何解决这个问题?所有其他变量都可以工作,但是多维数组。

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

2 个答案:

答案 0 :(得分:1)

您需要为每个维度指定一组方括号,例如:

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

答案 1 :(得分:0)

只需添加一对额外的括号:

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