简体   繁体   English

另一个与Arraylist相关的类的调用方法

[英]Calling method from another class which relates to Arraylist

I am building an application with a Room class which is abstract and a Standard class which inherits from Room . 我正在构建一个具有Room类的应用程序,该类是抽象的,而Standard类则继承自Room I have then created a Hostel class. 然后,我创建了一个Hostel类。 Within the Hostel class is ArrayList<Room> rooms to which rooms can be added. Hostel类中是可以添加ArrayList<Room> rooms I have created a method in the Hostel class which shows all available rooms but when I try and instantiate this in another class ( MainGUI ) nothing is shown. 我在Hostel类中创建了一个显示所有可用房间的方法,但是当我尝试在另一个类( MainGUI )中实例化此方法时,什么也没有显示。 As far as I can see this is because I am creating a new hostel each time I click the button but would like to know how to pass the data across instead of creating a new hostel each time. 据我所知,这是因为每次单击按钮时我都会创建一个新旅馆,但我想知道如何传递数据,而不是每次都创建一个新旅馆。 Below are the relevant snippets of code. 以下是相关的代码片段。

Hostel Class

public Hostel()
{
    rooms = new ArrayList<Room>();
}

public void showAvail()
{
    for (Room room : rooms)
    {
        if (room.available == true) 
        {
                 theString = room.getRoomData() + "\n";
                 //System.out.println("Available Rooms" + "\n" + theString);
                 JOptionPane.showMessageDialog(null,theString);
        }
    }
}

public void addRoom(Room theRoom)
{
    rooms.add(theRoom);
}

MainGUI Class

roomsFreeB.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        Hostel host = new Hostel();
        host.showAvail();
    }
});

Any help would be appreciated 任何帮助,将不胜感激

Unless you omitted code between the Hostel host = new Hostel(); 除非您省略了Hostel host = new Hostel();之间的代码Hostel host = new Hostel(); and host.showAvail(); host.showAvail(); , you never add any rooms to the hostel so there are no available rooms (or any at all) to show. ,您从不向旅馆添加任何房间,因此没有可供显示的房间(或任何房间)。 You need to either add rooms to host after you create it and before you showAvail , or create a Hostel instance variable, fill it somewhere, and then call showAvail on that. 您需要在创建host之后并且在showAvail之前添加房间作为host ,或者创建一个Hostel实例变量,将其填充到某个地方,然后在其上调用showAvail

Would be good to see how you initialize your rooms inside Hostel . 最好看看您如何在Hostel内初始化房间 And if you like to initialize Hostel once only, do it outside of Listener. 如果只想初始化一次Hostel,请在Listener之外进行。 In this case it must not be a field inside MainGUI. 在这种情况下,它一定不能是MainGUI中的字段。

 public void actionPerformed(ActionEvent e)
{
    Hostel host = new Hostel();
    host.showAvail();
}

In the previous code, the object is created and then destroyed once the method is done. 在前面的代码中,创建对象,然后在方法完成后销毁。 The variable host is a local variable and consequently lives only during the execution of the method. 变量宿主是局部变量,因此仅在方法执行期间存在。

Depending on what you want to do, you should declare your host variable inside the main method or declare an array of hostel inside the main method again. 根据您要执行的操作,应该在main方法内声明host变量,或者在main方法内再次声明hostel的数组。

Since you are creating new Hostel each time. 由于您每次都创建新的旅馆。 I guess there will be to Room s in the ArrayList. 我想ArrayList中会有Room

You need to create the Hostel Object outside the actionPerformed. 您需要在actionPerformed之外创建Hostel对象。 And in your case it should be created only one time. 并且在您的情况下,它应该只创建一次。 And on this created Hostel object you will be adding the Room object. 在此创建的Hostel对象上,您将添加Room对象。

If the question is where to do it.. Its left to you. 如果问题是在哪里做..它留给您。 Its upon your design. 它在您的设计上。

For eg it can be. 例如可以。

You can create a class called ABC inside which you can create the hostel object. 您可以创建一个称为ABC的类,在其中可以创建宿舍对象。 Write a static method called getHostel(). 编写一个名为getHostel()的静态方法。 Then call ABC.getHostel() 然后调用ABC.getHostel()

Yes you are right that everytime action is performed a new Hostel is created and so is the list of rooms associated with hostel. 是的,您没错,每次执行操作都会创建一个新的旅馆,因此与旅馆关联的房间列表也会被创建。

On click of button you would know which hostel you want to show (May be reading your application database or something), in case this is the first time your hostel will have empty room, else once you have read the hostel information you would also know about rooms which belong to the hostel, which can then be passed to your hostel object either through constructor or setter method. 单击该按钮,您将知道要显示哪个旅馆(可能正在读取应用程序数据库或其他内容),以防万一这是您旅馆第一次没有空房间,否则一旦您阅读了旅馆信息,您还将知道关于属于旅馆的房间的信息,然后可以通过构造函数或setter方法将其传递给您的旅馆对象。

Code snippet: 程式码片段:

createHostel(String hostelName) {
   //read from database
   //No hostel with hostelname found create a new hostel else if hostel is found send   the same (by this time hostel object would have room information also
}

Your action 你的行动

public void actionPerformed(ActionEvent actionEvent) {
   //MyFactory.getHostel(String hostelName)
   //Once you have hostel object call showAvail on it, if its new you will get nothing else you will get all the rooms available
}

Hope this gives you some insight. 希望这能给您一些见识。

Your problem is exactly what you thought, you are making a new ArrayList each time you click the button so you will never see the data. 您的问题正是您所想的,每次单击按钮都在创建一个新的ArrayList,这样您就永远看不到数据。 You should begin by creating a hostel object in your MainGUI class, 您应该首先在MainGUI类中创建一个hostel对象,

private Hostel hostel;

this will allow previously entered information to be referenced 这将允许引用先前输入的信息

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

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