如何声明一个arraylist集合类对象

时间:2013-12-10 16:54:08

标签: java

大家好我在这个浏览器类中创建了一个篮子字段但是,我现在需要在浏览器类中声明这个篮子字段作为arraylist集合类对象,但是无法弄清楚如何做到这一点,下面是我的代码到目前为止

 /**
 * Write a description of class Browser here.
 * 
* @author (johnson) 
 * @version (10/12/13)
 */
public class Browser
{
    // instance variables - replace the example below with your own
    private int iD;
    private String email;
    private int yearOfBirth;
    private boolean memberID;
    private WineCase wineCase;
    private boolean loggedIn;
    private Website website;
    private boolean discount;
    List<Boolean> basketList = new ArrayList<Boolean>();

 /**
     * Constructor for objects of class Browser
     */
    public Browser()
    {
        // initialise instance variables
        wineCase = null;
        website = null;
        iD = 00065;
        yearOfBirth = 1992;
        memberID = true;
        discount = false;
    }

 /**
  * Constructor for objects of class Browser
  */
 public Browser(String newEmail,int newYearOfBirth)
    {
        // initialise instance variables
        wineCase = null;
        website = null;
        iD = 0;
        email = newEmail;
        yearOfBirth = newYearOfBirth;
        loggedIn = false;
        memberID = true;
        discount = false;
    }

 /**
     * Constructor for objects of class Browser
     */
    public Browser(int newID, String newEmail,int newYearOfBirth)
    {
        // initialise instance variables
        wineCase = null;
        website = null;
        iD = newID;
        email = newEmail;
        yearOfBirth = newYearOfBirth;
        memberID = true;
        discount = false;
    }

     /**
     * returns the ID
     */

    public int getId()
    {
    return iD;
    }


    /**
     * gets the email of the browser class
     */

    public String getEmail()
    {
    return email;
    }

    public boolean getDiscount()
    {
        return discount;
    }


    /**
     * gets the yearOfBirth for the browser class
     */

    public int yearOfBirth()
    {
    return yearOfBirth;
    }

    public double getWineCost()
    {
        return wineCase.getWineCost();
    }

     /**
     * returns 
     */

    public void setLoginStatus(boolean status)
    {
       loggedIn = status;
    }

    /**
     * returns 
     */

    public void selectWineCase(WineCase winecase)
    {
    wineCase = winecase;
    System.out.println ("Browser "+getId()+" has selcted wine case"+wineCase.getRefNo()+ "of "+winecase.getNoOfBottles()+ wineCase.getDescription()+ " at £"+wineCase.getWineCost()); 
    }

     /**
     * returns
     */

    public void payForWine()
    {
      website.checkout(this);  
    }

    public void setId()
    {
        iD = 999;
    }

     public void setWebSite(Website website)
    {
        this.website = website;
    }

    public void setDiscount(boolean discount)
    {
        this.discount = discount;
    }
}

任何答案或回复都将不胜感激

4 个答案:

答案 0 :(得分:0)

试试这个

列表与LT;布尔&GT; basketList = new ArrayList&lt; Boolean&gt;();

答案 1 :(得分:0)

那不能是布尔值,因为布尔值是基本类型而不是对象的集合。 你需要做的是:

ArrayList basket = new ArrayList();

或者,如果您需要Boolean Objects(仍然不是布尔值)的集合,您可以这样做:

ArrayList basket = new ArrayList();

答案 2 :(得分:0)

要说出你在做什么有点难,但在课堂上声明这一点。另外,请不要忘记导入包java.util.ArrayListimport java.util.List

private List<Boolean> baskets = new ArrayList<Boolean>();

这将取代之前的声明:

private boolean Basket;

答案 3 :(得分:0)

布尔列表?

java.util.List<Boolean> basketAl = new java.util.ArrayList<Boolean>();

我建议你在课堂上添加一个toString方法

// Something like this...
public String toString() {
  StringBuilder sb = new StringBuilder();
  sb.append(iD).append(" ").append(email).append(" ");
  sb.append(yearOfBirth).append(" ").append(memberID).append(" ");
  sb.append(wineCase).append(" ").append(loggedIn).append(" ");
  sb.append(website).append(" ").append(discount).append(" ");
  sb.append(Basket);
  return sb.toString();
}