簡體   English   中英

如何將鏈表添加到鏈表的特定節點上?

[英]How to apend a linkedlist onto a specific node of a linkedlist?

我想知道如何將一個鏈表添加到另一個鏈表的特定索引中。 我正在嘗試做的圖片:

在此處輸入圖片說明

我要添加item到“約翰”。 item2到“ Amy”等

到目前為止,我的代碼:

import java.util.*;
public class LinkedListTest {
     public static void main(String args[]) {

         LinkedList<String> person = new LinkedList<String>();
         LinkedList<String> item = new LinkedList<String>();
         LinkedList<String> item2 = new LinkedList<String>();

         person.add("John");
         person.add("Amy");
         person.add("Bob");
         person.add("Michael");


         item.add("Eggs");
         item.add("Bread");
         item.add("Ham");

         item2.add("Toilet roll");
         item2.add("Eggs");


         for(int i = 0; i < item.size(); i++){
         person.addFirst(item.get(i));
         }
         System.out.println(person);

     }
}

您可以使用地圖來實現。 映射的每個鍵都代表“客戶”,而LinkedList可用於存儲每個客戶的“項目”。

public class LinkedListTest {
     public static void main(String args[]) {
         Map<String, LinkedList<String>> personItems = new LinkedHashMap<>();

         LinkedList<String> item = new LinkedList<String>();
         LinkedList<String> item2 = new LinkedList<String>();

         item.add("Eggs");
         item.add("Bread");
         item.add("Ham");

         item2.add("Toilet roll");
         item2.add("Eggs");

         personItems.put("John", item);
         personItems.put("Amy", item2);    


         for(String cust: personItems.keySet()){
             System.out.println(personItems.get(cust));
         }

     }
}

這有兩個優點:

  1. 您可以根據名稱查找客戶的物料清單。
  2. 另外,通過使用LinkedHashMap ,可以保留插入客戶的順序。

希望這可以幫助!

您想將一個人與項目列表相關聯 ,因此,make pair和Map是相同的,您可以在將對添加到Map中之前初始化List,我還添加了代碼以打印內容(請參見注釋代碼):

public static void main(String[] args) {
        HashMap<String,LinkedList<String>> person = new HashMap<String,LinkedList<String>>();
        LinkedList<String> item = new LinkedList<String>();
        LinkedList<String> item2 = new LinkedList<String>();

        //Add your people into the Map
        person.put("John",item);
        person.put("Amy",item2);
        person.put("Bob",new LinkedList<String>());
        person.put("Michael",new LinkedList<String>());

        //Add stuff to item for John
        item.add("Eggs");
        item.add("Bread");
        item.add("Ham");

        //Add stuff to item for Amy
        item2.add("Toilet roll");
        item2.add("Eggs");

        //To add stuff for Bob :
        person.get("Bob").add("Chocolate");

        //To add stuff for Michael :
        person.get("Michael").add("Chips");

        //To see what's inside for ONE person :
        for(String stuff : person.get("John")){
            System.out.println(stuff);
        }

        //To see ALL :
        for(String people : person.keySet()){
            System.out.println(people+" has bought :");
            for(String stuff : person.get(people)){
                System.out.println(stuff);
            }
        }
    }

而且實際上我不確定LinkedList是否必要,一個簡單的ArrayList可能就足夠了,也可以看一下: http : //www.sergiy.ca/img/doc/java-map-collection-cheat-sheet.gif

您正在將person定義為字符串的LinkedList。

因此很明顯,不能將LinkedList添加到字符串。

您可能有一個字符串鏈接列表的鏈接列表:

LinkedList<LinkedList<String>> person = new LinkedList<>();
LinkedList<String> item   = new LinkedList<String>();
LinkedList<String> item2  = new LinkedList<String>();

person.add(new LinkedList<>(Collections.singleton("John")));
person.add(new LinkedList<>(Collections.singleton("Amy")));
person.add(new LinkedList<>(Collections.singleton("Bob")));
person.add(new LinkedList<>(Collections.singleton("Michael")));

item.add("Eggs");
item.add("Bread");
item.add("Ham");

item2.add("Toilet roll");
item2.add("Eggs");

person.get(0).addAll(item);
person.get(1).addAll(item2);

System.out.println(person);

但隨后您將結束:

  • 將人的概念與項目的概念混合在一起。
  • 訪問特定人員時必須處理索引。

未來頭痛的最佳選擇。

首選選擇是Map<String, LinkedList<String>>以保持數據結構一致:

    Map<String, LinkedList<String>> person = new HashMap<>();
    LinkedList<String> item   = new LinkedList<String>();
    LinkedList<String> item2  = new LinkedList<String>();

    item.add("Eggs");
    item.add("Bread");
    item.add("Ham");

    item2.add("Toilet roll");
    item2.add("Eggs");

    person.put("John", item);
    person.put("Amy", item2);
    person.put("Bob", new LinkedList<>());
    person.put("Michael", new LinkedList<>());

    System.out.println(person);

由你決定。

面向對象的良好解決方案是:不要將字符串(人名)放入第一個鏈接列表。 而是使用personNameitemsBought字段創建一個Customer類。 后者本身就是對鏈接項目列表的引用。 現在為人員列出Customer對象。 然后,將鏈接的項目列表添加到每個客戶將變得很自然而直接。

public class Customer {

    String personName;
    List<String> itemsBought;

    public Customer(String personName) {
        this.personName = personName;
        itemsBought = Collections.emptyList();
    }

    public void setItemsBought(List<String> itemsBought) {
        this.itemsBought = itemsBought;
    }

    @Override
    public String toString() {
        return personName + " bought " + itemsBought;
    }

}

通過本課程,您可以:

    LinkedList<Customer> person = new LinkedList<>();
    LinkedList<String> item = new LinkedList<String>();
    LinkedList<String> item2 = new LinkedList<String>();

    person.add(new Customer("John"));
    person.add(new Customer("Amy"));
    person.add(new Customer("Bob"));
    person.add(new Customer("Michael"));


    item.add("Eggs");
    item.add("Bread");
    item.add("Ham");

    item2.add("Toilet roll");
    item2.add("Eggs");

    person.get(0).setItemsBought(item);
    person.get(1).setItemsBought(item2);

    System.out.println(person);

打印:

[John bought [Eggs, Bread, Ham], Amy bought [Toilet roll, Eggs], Bob bought [], Michael bought []]

我會發現自底向上構建列表會更方便:在將客戶添加到人員列表之前,先將項目添加到客戶。 我們可以使用同時接受名稱和項目的varargs構造函數來添加更多便利。 想想我們是否可以做:

    person.add(new Customer("John", "Eggs", "Bread", "Ham"));
    person.add(new Customer("Amy", "Toilet roll", "Eggs"));
    person.add(new Customer("Bob"));
    // etc.

如果將Customer構造函數更改為以下內容,則可以:

public Customer(String personName, String... itemsBought) {
    this.personName = personName;
    this.itemsBought = new LinkedList<>(Arrays.asList(itemsBought));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM