如何在Java中的构造函数类中为多维数组分配内存

时间:2015-04-14 22:12:35

标签: java memory multidimensional-array constructor allocation

当我将数组字段创建为私有double []平衡时,我可以通过balance = new double [] {n,u,m,b,e,r,s};在构造函数类中分配内存。但是,当我尝试为多维数组分配内存时,它将无法工作。 netbeans IDE中的错误说我无法将int转换为double。所以我很困惑为什么会这样。任何帮助,将不胜感激。这是因为我不包括.00的全部金额(即12而不是12.00)?

这是我的示例代码:

package myatmexample;


enum AccountType
{
    CHECKINGACCOUNT,
    SAVINGSACCOUNT,
    CDACCOUNT,
};

public class bankUserData
{
private final String []  users;
private AccountType [] userAccounts;
private double [][][] balance;

public bankUserData()
{
    users = new String []
    {
        "Jim Beam", "Bailey Irish-Cream", "Jack Daniels","Grey Goose", "Ginger Skyy", "Marga Rita",
        "Train Wreck", "OG Skywalker", "Flo Green", "Polly Pak", "Princess Diesel","Gracie Slick"
    };

    userAccounts = new AccountType[]
    {
        AccountType.CHECKINGACCOUNT,  
        AccountType.SAVINGSACCOUNT,
        AccountType.CDACCOUNT
    };

    balance = new double [][][]
    {
        { 350, 435, 796, 82.43, 1003.50, 2500, .50, 365, 892, 134, 768, 25892 },
        { 43, 58, 98, 100,54, 33.25, 65.5, 89.7, 71.8, 34.3, 45,67 },
        { 21,23,45,67,68,69,43,21,44,56.78,59, 64 }
    };

    Object [] fullAccountArray = { users, userAccounts };
}
}

2 个答案:

答案 0 :(得分:0)

balance= new double [][][]   {{350, 435, 796, 82.43, 1003.50, 2500, .50,  
365, 892, 134, 768, 25892},
{43, 58, 98, 100,54, 33.25, 65.5, 89.7, 71.8, 34.3, 45,67},
{21,23,45,67,68,69,43,21,44,56.78,59, 64}};

初始化无效。需要3D矩阵,但您只在括号内创建2D矩阵({{...}}应为{{{...}}})。

答案 1 :(得分:0)

你有

中的双打数组
{{350, 435, 796, 82.43, 1003.50, 2500, .50, 365, 892, 134, 768, 25892},
{43, 58, 98, 100,54, 33.25, 65.5, 89.7, 71.8, 34.3, 45,67},
{21,23,45,67,68,69,43,21,44,56.78,59, 64}};

当你声明new double [][][]:一组双打阵列数组时。 因此,在350的位置,它需要一个双精度数组而不是双精度数。

希望它不会混淆;)