gpt4 book ai didi

java - 显示数组列表的元素

转载 作者:行者123 更新时间:2023-11-30 04:04:17 24 4
gpt4 key购买 nike

我正在尝试显示我的 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));
}


}

}

                                   `                                                                                                                                           

最佳答案

看看你的电话:

library.printLibrary();

现在看看声明:

public static void printLibrary(ArrayList<LibraryItem>items)

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

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

public void printLibrary()

...保持方法主体相同,然后它将获取实例变量items

关于java - 显示数组列表的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21142317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com