2017-02-22 54 views
1

我想为链接列表中的每个乘客输入一组新节点。 例如:如何在链接列表中为每个单独的链接列表输入一组新节点

对于我输入的乘客名称,比如约翰。

对于国家代码我输入:BI

对于航班号我输入:095

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

比方说,我进入:约翰,BI,095,3

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

然后我按'b'输入新的乘客。

然后我下一个乘客进入:简,BU,096,3

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

如何删除旧节点(BI0950,BI0951,BI0952) baggage每当我点击'b'('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; 

    } 
    } 
} 

回答

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; 
} 
+0

你好,我已经做到了,但不知何故它只打印最新的节点。这是我会得到的'[John with baggage(s)[BI0952]] ' –

+0

它将继续替换现有的节点,因为每次该方法被称为'baggagex'将被清空 –