【问题标题】:How do I enter a new set of nodes in a linkedlist for each individual linkedlist如何在链表中为每个单独的链表输入一组新节点
【发布时间】:2017-02-22 06:04:17
【问题描述】:

我想在链表中为每位乘客输入一组新节点。 例如:

对于乘客姓名,我输入 John。

对于国家代码,我输入:BI

我输入航班号:095

我可以输入任何数量的行李数量。

假设我输入:John, BI, 095, 3。

这是我得到的:[John with baggage(s) [BI0950, BI0951, BI0952]] 这是我想要的。

然后我按“b”输入新乘客。

然后我为下一位乘客输入:Jane, BU, 096, 3。

这是我得到的:[John with baggage(s) [BI0950, BI0951, BI0952], Jane with baggage(s) [BI0950, BI0951, BI0952, BU0960, BU0961, BU0962]]

每当我点击“b”时,如何从baggage Linkedlist 中删除旧节点(BI0950、BI0951、BI0952)(“b”是添加新乘客)?

import java.util.*;

public class baggage_system{

  static LinkedList<String> baggagex = new LinkedList<String>();  

  public static String getUser_command(){
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter command B-baggage, n-next, q-quit");
    String s = keyboard.nextLine();
    return s;
  }

  public static String getUser_flight(){
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Please enter the flight number");
     String s = keyboard.nextLine();
     return s;
  }

  public static String getPassenger(){
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Please enter passenger name");
     String s = keyboard.nextLine();
     return s;
  }

   public static String getUser_country(){
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Please enter the country code");
     String s = keyboard.nextLine();
     return s;
  }

   public static int getUser_number(){
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Please enter number of baggage");
     int s = keyboard.nextInt();
     return s;
  }


   public static String nextbg(ListIterator<String> iteratorbg){   
     return iteratorbg.next();      
   }

   public static LinkedList<String> makeBaggage(String country, String flight, int num){

     baggagex.add(country + flight + num);

     return baggagex;

   }

   public static int count(){
     System.out.println(baggagex.size());
     return baggagex.size();

   }

  public static void main(String args[]) {

    LinkedList<Passenger> passenger = new LinkedList<Passenger>();
    LinkedList<String> baggage = new LinkedList<String>();
    ListIterator<String> iteratorbg = baggage.listIterator();

      LinkedList<String> counting = new LinkedList<String>();

    String command = "";

    while (!command.equals("q")){
      command = getUser_command();

     if(command.equals("B") || command.equals("b")){
       String p = "";
       p = getPassenger();
       passenger.add(new Passenger(p));


       String country = "";
       country = getUser_country();

       String flight = "";
       flight = getUser_flight();

       int amount = 0;
       amount = getUser_number();

      String[] bg = new String[amount];



      for(int i = 0; i < amount; i++){
  //      LinkedList<String> bgg = new LinkedList<String>();

        baggage = makeBaggage(country, flight, i);

        System.out.println(baggage);

       }
      LinkedList<String> bgg = new LinkedList<String>(baggage);



      passenger.getLast().setBaggages(bgg);


      System.out.println(passenger);

/*      if(baggage.size() != 0 && baggage.size() > baggage.size() - 1){
        for(int j = 0; j < bgg.size(); j++){
          baggage.remove(j);
        } 
      */
//      System.out.println(bgg);



/*       while (!baggage.isEmpty()) {
        baggage.removeFirst();
    }
       */

     } else if(command.equals("n")){
       count();
     } else if (command.equals("p")){
       System.out.println(baggage.peekFirst());
     }

     else
       System.out.println("Enter 'q' to end the program");

     }
  }

public static class Passenger {

    String passengers;    
    List<String> baggage;

    public Passenger(String passengers) {
        this.passengers = passengers;
        baggage = null;
    }

    public void setBaggages(List<String> baggages) {   
        this.baggage = baggages;
    }

    public void pBags(){

    }

    @Override
    public String toString() {      

        return passengers + " with baggage(s) " + baggage;

    }
  }
}

【问题讨论】:

    标签: java linked-list nodes


    【解决方案1】:

    问题在于您的编码逻辑,您对所有乘客使用相同的链表行李对象。

    static LinkedList<String> baggagex = new LinkedList<String>();  
    

    如果您在此 LinkedList 中添加或删除任何对象,它将影响所有乘客。

    解决方案: 为每位乘客使用一个新的链表对象。如下代码:

    public static LinkedList<String> makeBaggage(String country, String flight, int num)
    {
         LinkedList baggagex = new LinkedList();
         baggagex.add(country + flight + num);
    
         return baggagex;
    }
    

    【讨论】:

    • 您好,我已经这样做了,但不知何故它只打印了最新的节点。这就是我会得到的[John with baggage(s) [BI0952]]
    • 它将继续替换现有节点,因为每次调用该方法时baggagex 都会被清空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 2018-10-02
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多