2013-10-26 34 views
-1

我已经创建了我的链表,我希望用户输入一个站,然​​后输出是为该站存储的号码。搜索链接列表用户输入 - Java

LinkedList myList = new LinkedList(); 



      myList.addFirst("London", 5);    
     myList.addNode("Manchester ", 10); 

     myList.addNode("Liverpool", 20); 
     myList .addNode("Birmingham", 50); 

这是用户输入的输入。

  String name;    
       name = JOptionPane.showInputDialog("Enter Station: "); 


    StringNode temp; 

     temp = mylist.head; 

     if (temp.Station == (name)) { 


      System.out.println("Yes"); 


     } 

其余的方法只是添加一个新的数据和打印。

谢谢

+0

我试过,但对于链接列表不工作 – user2221029

+1

再试一次。 temp.Station ==(name)应该是temp.Station.equals(name)。这是假设站是一个字符串。 – tom

回答

1

这个问题是一个HashMap的用途。

Map<String, Integer> map = new HashMap<>(); 
map.put("London", 5);    
map.put("Manchester ", 10); 
map.put("Liverpool", 20); 
map.put("Birmingham", 50); 

String station = "Liverpool"; 
Integer i = map.get(station); 
System.out.println(i);