显示arraylist的元素

时间:2014-01-15 16:05:42

标签: java eclipse

我试图通过从另一个类调用ArrayList<LibraryItem>方法在动作侦听器方法中显示printlibrary的元素。我一直在说:

The method printLibrary(ArrayList<LibraryItem>) in the type Library is not applicable for the arguments.

我不明白我做错了你能不能帮助我。

import java.util.ArrayList;

public class Library {

    private ArrayList<LibraryItem> items;

    public Library() {
        items = new ArrayList<LibraryItem>();
    }

    public void addItem(LibraryItem newItem) {
        items.add(newItem);
    }

    public LibraryItem searchForItem (String name) {
        for(LibraryItem searchForItem: items) {
            if(searchForItem.getName().equals(name))
                return searchForItem;
        }
        return null;
    }

    public static void printLibrary(ArrayList<LibraryItem> items) {
        for(int i = 0; i < items.size(); i++) {
            System.out.println(items.get(i));
        }
    }
}

public void actionPerformed(ActionEvent ev) {
    String t = title.getText();     
    int y = Integer.parseInt(year.getText());
    int q = Integer.parseInt(quantity.getText());

    library.addItem(new LibraryItem(t,y,q)); 

    library.printLibrary(); -------->>>> ERROR HERE!!!
}                                                                                                     this is the edited version and the error                                               public void printLibrary(ArrayList<LibraryItem> items) {
        for(int i = 0; i < items.size(); i++) {
            System.out.println(items.get(i));
        }
    }
}      public void actionPerformed(ActionEvent ev) {
    String t = title.getText();     
    int y = Integer.parseInt(year.getText());
    int q = Integer.parseInt(quantity.getText());

    library.addItem(new LibraryItem(t,y,q)); 

    library.printLibrary(); 
}                                                                                              this is my code for libraryitem class `                                                                public class LibraryItem 

{

    private String name;
    private int year, quantity;

   LibraryItem(String nameIn, int yearIn, int quantityIn)
   {
       name = nameIn;
       year = yearIn;
       quantity = quantityIn;



   }



public boolean rent()
{


    if(quantity > 0) 

    {
         quantity--;

    }
      return true;
}






    public String toString()
   {
return name + " " +"has"+ " " +quantity+ "books on loan";


   }

    public String getName()
   {
return name;

   }


   public int getQuantity()
   {

   return quantity;
   }

   public int getYear()
   {

   return year;
   }


   }

`

这是我的库`import java.util.ArrayList;

的代码
 public class Library {

private ArrayList<LibraryItem> items;




public Library()
{
    items = new ArrayList<LibraryItem>();
}



public void addItem(LibraryItem newItem)
{
    items.add(newItem);

}

public LibraryItem searchForItem (String name)

{
    for(LibraryItem searchForItem: items)
    {
        if(searchForItem.getName().equals(name) )

    return searchForItem;


}
    return null;
}

public void printLibrary()
{


    for(int i = 0; i< items.size(); i++)
    {

        System.out.println(items.get(i));
    }


}

}

                                   `                                                                                                                                           

2 个答案:

答案 0 :(得分:2)

看看你的电话:

library.printLibrary();

现在看一下宣言:

public static void printLibrary(ArrayList<LibraryItem>items)

这是一个静态方法,其参数是一个列表。您试图将其称为实例方法(不幸的是,即使在调用静态方法时Java也允许这样做,但无论如何都应该避免这种方法)但不提供任何参数。

鉴于Library已经有一个项目列表,我建议您只需更改声明,使其成为无参数实例方法:

public void printLibrary()

...让方法的主体保持不变,然后选择实例变量items

答案 1 :(得分:0)

您已声明printLibrary(ArrayList<LibraryItem> items),但您在不传递任何参数的情况下调用它。

宣言:

public static void printLibrary(ArrayList<LibraryItem>items)
{
    for(int i = 0; i< items.size(); i++)
    {
        System.out.println(items.get(i));
    }
}

对它的呼唤:

library.printLibrary();

对printLibrary()方法的调用需要一个ArrayList,所以你想要调用它:

library.printLibrary(items);

但是,如果您所做的只是从类的私有数据成员打印出信息,则应更改方法的声明方式。更好的声明是:

public void printLibrary()
{
    for(int i = 0; i< items.size(); i++)
    {
        System.out.println(items.get(i));
    }
}

现在,您图书馆的printLibrary()方法将打印items数据成员中的每个项目。如果您将声明更改为此声明,则可以将呼叫保留为library.printLibrary()