gpt4 book ai didi

java - LinkedList中的return null基本上就是return null

转载 作者:行者123 更新时间:2023-12-02 08:40:39 24 4
gpt4 key购买 nike

我有三个类别:Customer、Store 和 items

我正在使用“LinkedList”在类之间移动。

元素类别

public class Item {
public Item(String code, String name) {
this.code = code;
this.name = name;
}

@Override
public String toString() {
return name + " : " + code;
}

}

商店类

    public class Store {
private LinkedList<Item> items = new LinkedList<Item>();

public Store(String name, String number) {
this.name = name;
this.number = number;
}

public void addItem(String code, String name){
items.add(new Item(this, code, name);
}

public LinkedList<Item> viewItem(){
for(int j = 0 ; j < items.size(); j++)
System.out.println(items.get(j))
return null; // I though return null you just return nothing. But it literally return null;
}
}

客户

   public class Customer {
private LinkedList<Store> stores = new LinkedList<Store>();

public Customer() {
stores.add(new Store("Game", "1"));
stores.add(new Store("Grocery", "2"));

stores.get(0).addItem("001A", "GTA");
stores.get(0).addItem("001B", "GOD OF WARS");
stores.get(0).addItem("001C", "THE LAST OF US");

stores.get(1).addItem("002A", "Sandwich");
stores.get(1).addItem("002B", "Cup Noodle");
stores.get(1).addItem("002C", "Ice Cream");
}

public static void main(String args[]) {
new Customer().view();
}

public void view() {
System.out.println(stores.get(0).viewItem());
}

}

我只想返回

GTA : 001A
GODS OF WARS : 001B
THE LAST OF US: 001C

但它也返回“null”我怎样才能消除它。

我尝试只使用 Activity,但它只返回一个。

我无法删除 LinkedList,因为它必须返回一系列视频游戏。

谁能给我解释一下吗,谢谢。

最佳答案

在函数 Customer.view 中,您将打印函数 Store.viewItem 的结果。由于您的 viewItem 函数返回 null,因此该 print 语句将打印 null

只需删除 print 语句,您就应该得到您想要的内容:

public void view(){
stores.get(0).viewItem();
}

关于java - LinkedList中的return null基本上就是return null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61409771/

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