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

Java 如何将类中的项添加到ArrayList?,java,arraylist,methods,static,Java,Arraylist,Methods,Static,我知道这个问题看起来很奇怪,但我会尽力解释清楚。我正在做一个游乐园项目,在这个项目中,你可以使用票证、商品等的方法。我用这些方法创建了一个票证类,但现在我在游乐园类中尝试创建一个方法,从该类中获取日期并将其放入一个新的ArrayList中。也许我的代码能帮你解释一下 首先,这是我的机票等级 import java.text.DecimalFormat; public class Ticket { private long number; private Stri

我知道这个问题看起来很奇怪,但我会尽力解释清楚。我正在做一个游乐园项目,在这个项目中,你可以使用票证、商品等的方法。我用这些方法创建了一个票证类,但现在我在游乐园类中尝试创建一个方法,从该类中获取日期并将其放入一个新的ArrayList中。也许我的代码能帮你解释一下

首先,这是我的机票等级

    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;
    }
    }
下面是一些伪代码,解释了我将要发布的下一个类的操作

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类创建ArrayList。
//票证类具有以下构造函数。。。。
//(长类型的票号、字符串类型的类别、字符串类型的持票人、入场日期、双类型的购买价格、名为“已购买”的变量是否已支付布尔类型的票)
//class类型的变量之一是tickets,其中ticket类被制作成ArrayList。
//下一个任务是获取可用日期的票证,这是通过搜索未完成购买的票证来完成的。
创建名为getTicketDates()的公共ArrayList方法{
创建一个名为DateArray的变量,它是一个新的ArrayList;
For(从列表的第一个位置开始,遍历整个列表,递增1){
if(票证数组列表的布尔值为false)**{
将对象的日期从Ticket ArrayList添加到DateArrayList。}**//此操作将所有尚未购买的Ticket的日期存储到新ArrayList中。
}
返回日期数组;
}
//下一个任务是在date数组中只搜索选定的日期,并将该日期的可用票证作为整数发布。
创建一个方法,通过遍历date数组(日期)显示指定日期的票证数量{
For(从日期数组的第一个位置开始,浏览整个列表并查找具有特定日期的票证){
如果(日期==输入的日期){
将该车票作为该日期的可用车票之一。
}
}
以整数类型返回该日期的可用票证总数。
}
好的,现在是我的娱乐公园课程。注意,它还没有完成。我只是想把这一部分做完

        import java.util.ArrayList;
    import java.util.Date;
    public class AmusementPark {
    private ArrayList<Ticket> tickets;
    private ArrayList<Merchandise> merchandise;
    private String name;
    AmusementPark(String name){
        this.name=name;
        this.tickets = new ArrayList<Ticket>();
        this.merchandise= new ArrayList<Merchandise>();
    }
    String getName(){
        return name;



    }
     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;
    }
    }
import java.util.ArrayList;
导入java.util.Date;
公共娱乐公园{
私人ArrayList门票;
私人ArrayList商品;
私有字符串名称;
娱乐公园(字符串名称){
this.name=name;
this.tickets=new ArrayList();
this.products=new ArrayList();
}
字符串getName(){
返回名称;
}
公共数组列表getTicketDates(){
ArrayList theDateArray=新的ArrayList();
int i;
String date=Ticket.getDate();//这不起作用。请参阅下面的原因。

对于(i=0;i让我们采用你的方法

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 ticket;
for (i=0; i<tickets.size(); i++){
    ticket = tickets.get(i);
    if(ticket.getPurchased()== false){
        theDateArray.add(ticket.getDate());
    }

}
客票;

对于(i=0;i如果我理解正确,这里需要的是将未购买票证的日期提取到单独的日期数组中。并且您已经在伪代码中正确地获得了它,您只需要在实现过程中更严格地遵循它:

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;
}
public ArrayList getTicketDates(){
ArrayList theDateArray=新的ArrayList();
//迭代所有票据
票(票:票){
//如果不买票
如果(!ticket.getPurchased()){
//将票据日期添加到数组中
add(ticket.getDate());
}
}
返回日期数组;
}

不要进行静态引用!与其写一个什么是代码的答案,不如把重点放在问题的解决方法上。 当您将值设置为票证类的实例变量时,它将引用一个特定的对象(new TICKET())来访问它的值。如果您将类的变量设置为staticValues,它将在加载类时存储,而不是在创建对象时存储,但需要具有arraylist项 票证类别的对象

应该采用一种简单的方法 在创建TICKET类的对象时,通过在其构造函数中传递值,然后 将这些对象添加到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
List tList=new ArrayList();
tList.add(t1);
t增加(t2);
//等等
现在从arralylist中检索值
票证t=tlist.get(0);
t、 猫;
t、 价值是什么

如果迭代列表中的
Ticket
,则需要为每个实例调用
getDate
。因此
datearray.add(tickets.get(i).getDate())
因为您需要特定实例的日期。顺便说一句,这个问题太长了,您应该能够解释,您不能调用方法来获取值,因为它不是静态的。请参阅不要生成静态引用!!将类的对象和设置的方法剪切值生成该对象,然后在arraylist中传递该对象。每个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