在LinkedList中返回null基本上是返回null

我有客户,商店和物品三类

我正在使用“ 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(i))
           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,因为它必须返回一系列视频游戏。

有人可以向我解释谢谢。

评论
  • 离心咒
    离心咒 回复

    In the function Customer.view, you are printing out the result of the function Store.viewItem. And because your viewItem function returns null, that print statement is going to print null.

    只需删除打印声明,您将获得所需的内容:

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