如何将类中的项添加到ArrayList?

时间:2017-04-28 05:34:21

标签: java arraylist methods static

我知道这个问题看起来很奇怪,但我会尽力解释它。我正在做一个游乐园项目,你有票据,商品等的方法。我用方法制作了一个Ticket类,但现在我在AmusementPark类试图创建一个从该类获取日期的方法将它放入一个新的ArrayList中。也许我的代码会帮助解释它。

首先,这是我的Ticket类......

    ancestry.forEach(function(person) {
      var ages = {};
      var century = Math.ceil(person.died / 100);

      if (century in ages) 
        {
        ages[century].push(person.died - person.born);
            }
      else
           {       
        ages[century] = [person.died - person.born];
           }
    });

以下是一些伪代码,解释了我想要发布的下一堂课的内容。

    import java.text.DecimalFormat;

    public class Ticket {
    private long number;
    private String category; 
    private String holder;
    private String date;
    private double price;
    private boolean purchased;
    Ticket(long num, String cat, String h, String dt, double pr, boolean pch){
        this.number= num;
        this.category= cat;
        this.holder= h;
        this.date= dt;
        this.price= pr;
        this.purchased= pch;

    }
    long getNumber(){
        return number;
    }
    String getCategory(){
        return category;
    }
    String getHolder(){
        return holder;
    }
     String getDate(){
        return date;
    }
    boolean getPurchased(){
        return purchased;
    }
    double getPrice(){
        return price;
    }
    void setPrice(double pr){
        price= pr;
    }
    void setChangePurchased(boolean newStatus){
        purchased= newStatus;
    }
    @Override
    public String toString(){
        DecimalFormat dm= new DecimalFormat("#.##");

        String disp;
        disp = "Number: " + getNumber() + "\nCategory: " + getCategory() + "\nTicket Holder Name: " + getHolder() + "\nDate: " + getDate()
        + "\nPrice: " + dm.format(getPrice()) + "\nPuchased Completed?: " + purchased;
        return disp;
    }
    }

好的,现在这是我的AmusementPark课程。 注意尚未完成。我只是想完成这一部分....

Create an ArrayList from the Ticket class.
 //The ticket class has the following constructors....
 // (Ticket number of type long, category of type String, Ticket holder of type String, Date of admission, purchase price of type double, variable named "purchased" whether the ticket has been paid for of type boolean)

 //One of the variables of type class is tickets in which the ticket class is made into an ArrayList. 
 //The next task is to get tickets for dates where they are available, which is done by searching tickets where the purchase is not completed. 

 Create a public ArrayList<Date> method called getTicketDates(){
    Create a variable called theDateArray which is a new ArrayList<Date>;
    For(starting at the first position of the list, go through the the entire list incrementing by one){
      if (boolean purchased of the Ticket ArrayList is false)**{
        Add the date of the object from the Ticket ArrayList to theDateArray ArrayList.}**    //This stores the dates of all tickets not yet purchased into the new ArrayList.  
    }
      Return theDateArray;
 }
   //The next task is to search through theDateArray for only select dates and post the available tickets for that date as an integer.  

  Create a method which displays the number of tickets for a specified date by going through theDateArray (Date date) {
     For(starting at the first position of theDateArray, go through the entire list and look for tickets that have a particular date){
        if (the date== entered date){
        Include the ticket as one of the tickets available for that date. 
        }

 }
     Return the total number of tickets available for that date as a type integer.  
 }

好的,现在当我尝试从Ticket类调用getDate()方法时会发生什么,它不允许我使用它,因为我无法对非静态方法进行静态引用。但是,当我尝试使该方法保持静态时,它通过说我不能对非静态字段进行静态引用而弄乱了另一个类。

已经制作了Ticket类的ArrayList。我需要它滚动浏览该列表,获取布尔值为false的那些,并将日期添加到下一个ArrayList。

这一点有意义吗?

任何更好的想法?

3 个答案:

答案 0 :(得分:0)

让我们采取你的方法。

public ArrayList<String> getTicketDates(){
    ArrayList<String> theDateArray= new ArrayList<>();
    int i;
    String date = Ticket.getDate();    //This is not working.  See Reason Below.

    for (i=0; i<tickets.size(); i++){
        if(tickets.get(i).getPurchased()== false){
            theDateArray.add(date);
        }

    }
    return theDateArray;
}

注意你试图在一个实例上做的Ticket.getDate()上的问题,所以静态调用。但是,您解释的是,您希望列表Ticket的{​​{1}}的日期。好的,你正在迭代它,但推动这个奇怪的值tickets来自“静态方法”。

问题在于,您想要的date实例位于列表中。您正在使用它来查看是否已购买。因此,在这些实例上调用方法来获取值:

date

但更好:

for (i=0; i<tickets.size(); i++){
    if(tickets.get(i).getPurchased()== false){
        theDateArray.add(tickets.get(i).getDate());
    }

}

答案 1 :(得分:0)

如果我理解正确,这里需要的是将未购买门票的日期提取到单独的日期数组中。你已经在你的伪代码中正确地得到了它,你只需要在实现过程中更严格地遵循它:

public ArrayList<String> getTicketDates() {
    ArrayList<String> theDateArray = new ArrayList<>();

    // iterate over all tickets
    for ( Ticket ticket : tickets ) {
        // if ticket not purchased
        if ( ! ticket.getPurchased() ) {
            // add ticket's date into array
            theDateArray.add( ticket.getDate() );
        }
    }
    return theDateArray;
}

答案 2 :(得分:0)

不要进行静态参考!不要写出什么样的代码答案,而是要关注问题的解决方法。     当你将值设置为TICKET类的实例变量时,它将引用一个特定的对象(new ticket())来访问它的值,如果你创建类staticValues的变量将在加载类时存储而不是在创建对象时存储但是arraylist物品需要有                 票类对象。

应该采用一种简单的方法                  当您通过在其构造函数中传递价值而使得门票类别成为对象时,将值分配给变量                 将这些对象添加到ARRAYLISTITEMS

   ticket class

public class Ticket {
private long number;
private String category; 
private String holder;
private String date;
private double price;
private boolean purchased;
Ticket(long num, String cat, String h, String dt, double pr, boolean pch){
    this.number= num;
    this.category= cat;
    this.holder= h;
    this.date= dt;
    this.price= pr;
    this.purchased= pch;

}
 make a new object and pass values
   Ticket t1=new Ticket(3,"yourstring","yourstring",yourDouble,true/false);

在arrayliSt中添加项目:

    List<Tickets> tList=new ArrayList();
               tList.add(t1);
              tList.add(t2);
               //and so on

 now retrive values from arralylist 
            Ticket t=tlist.get(0);
            t.cat;
            t.whatevrbe thevalue be