简体   繁体   English

将在ArrayList中创建的对象传递给另一个类的另一个方法

[英]Passing objects created in ArrayList to another method of another class

I have a class named AirMain where I took in inputs from the user of airline information. 我有一个名为AirMain的类,我在其中接收航空公司信息用户的输入。 I have another class named Airline where constructors are created to store the user input as as Airline object which then store into ArrayList named "info". 我还有另一个名为Airline的类,在其中创建了构造函数,以将用户输入作为Airline对象存储,然后将其存储到名为“ info”的ArrayList中。

I have another ArrayList named "query" storing objects from Airline class but different parameter from the "info" ArrayList. 我有另一个名为“查询”的ArrayList,用于存储Airline类的对象,但参数与“ info” ArrayList不同。

When I tried to pass both of these ArrayList to another class method (named earliestDepart() in Airline class), I receive error "Cannot find symbol; location: class Object". 当我试图将这两个ArrayList都传递给另一个类方法(在Airline类中命名为earlyesttDepart())时,收到错误消息“找不到符号;位置:类Object”。 It is quite a lengthy code, so please see below for a snapshot of the passing to other method of another class and the point where error happens. 这是一个很长的代码,因此请参见下面的快照,以了解传递给另一个类的其他方法以及发生错误的地方。

Please advise me how do I pass Arraylist containing of objects to another class for process. 请告诉我如何将包含对象的Arraylist传递给另一个类进行处理。 Thanks in advance! 提前致谢!

Code in AirMain Class AirMain类中的代码

ArrayList <Airline> info = new ArrayList<Airline>();
ArrayList <Airline> query = new ArrayList<Airline>();
Airline createAirline = new Airline (fromCity, toCity, departTime, arriveTime, cost);
info.add(createAirline);
Airline createQuery = new Airline(type, fromCity, toCity);
query.add(createQuery);
if (query.get(i).getType() == 1) { query.get(i).earliestDepart(info);  }//end of if type 1

Code in Airline Class 航空舱代码

public void earliestDepart(ArrayList info){
String retrieve = info.get(i).getFromCity();//error kicks into this statement 
}

You need to modify the info argument in your earliestDepart() method like this: 您需要像这样在earliestDepart()方法中修改info参数:

public void earliestDepart(List<Airline> info){
String retrieve = info.get(i).getFromCity();//error kicks into this statement 
}

将值传递给earliestDepart方法。

public void earliestDepart(List<Airline> infoList){...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM