无法将对象的ArrayList从一个类传递到另一个类

时间:2016-03-07 03:55:10

标签: java user-interface arraylist

我正在尝试在JTextArea上显示两个ArrayLists,但是将对象的ArrayList从一个类传递到另一个类时遇到了麻烦。

我遇到问题的部分代码是尝试使用以下代码显示信息

private void jButtonDisplayActionPerformed(java.awt.event.ActionEvent evt)                                               
    {                                                   
        for(int i=0; i<bars.size(); i++)
        {
            jTextAreaDisplay.append(jTextAreaDisplay.getText() 
                    + bars.get(i).toString() + bars.get(i).getLiquor.toString() 
                    + "\n\n");

        }
    }      

它说它无法从我的Bar类中找到符号:变量getLiquor。

Bar类代码,我试图传递它:

ArrayList<Liquor> liquors;
private Liquor liquor = new Liquor(liquors);
public Liquor getLiquor()
    {
        return liquor;
    }

来自我的其他班级酒:

ArrayList<Liquor> liquors = new ArrayList<>();  
public Liquor(ArrayList<Liquor> liquors)
{
    this.liquors = liquors;
    this.vodka = getV();
    this.whiskey = getW();
    this.rum = getR();
    this.gin = getG();
    this.brandy = getB();
    this.vCount = getVC();
    this.wCount = getWC();
    this.rCount = getRC();
    this.gCount = getGC();
    this.bCount = getBC();
}

我的酒类代码中的另一个构造函数(如果相关):

public Liquor(String vodka, String whiskey, String rum, String gin, 
            String brandy, int v, int w, int r, int g, int b)
    {
        this.vodka = vodka;
        this.whiskey = whiskey;
        this.rum = rum;
        this.gin = gin;
        this.brandy = brandy;
        this.vCount = v;
        this.wCount = w;
        this.rCount = r;
        this.gCount = g;
        this.bCount = b;
    }

更新

从我所看到的情况来看,我稍微更改了一下,至少显示了酒的名称,但每个的库存号都没有被读入,而是显示为空。我认为问题在于:

public Liquor(ArrayList<Liquor> liquors)
    {

        this.vodka = "Vodka";
        this.whiskey = "Whiskey";
        this.rum = "Rum";
        this.gin = "Gin";
        this.brandy = "Brandy";
        this.vCount = getVC();
        this.wCount = getWC();
        this.rCount = getRC();
        this.gCount = getGC();
        this.bCount = getBC();
    }

我认为我的吸气剂工作正常。或者也许是ArrayList?

输出:

  

Kendall酒吧名为:Kink   现场音乐:真实   食品服务:虚假   目前有现货的酒:

     

伏特加:0

     

威士忌:0

     

朗姆酒:0

     杜松子酒:0

     

白兰地:0

1 个答案:

答案 0 :(得分:0)

您通过getLiquor方法调用错过了括号:

bars.get(i).toString() + bars.get(i).getLiquor().toString()