2011-04-22 264 views
9

我有一个项目,我正在为我的Java类(显然)工作,我一定错过了关于如何与TreeMaps进行交互的讲座。我不知道我在做什么,并且我没有从Google获得很多帮助。如何打印TreeMap中的所有值?

对于程序中的第一种情况,我必须打印TreeMap的所有值。以下是我提供的代码以及我已完成的工作。情况A中的一切都是我的,但它不起作用。任何帮助,将不胜感激。

import java.util.Scanner; 
import java.util.Set; 
import java.util.Map; 
import java.util.TreeMap; 
import java.io.File; 
import java.io.FileNotFoundException; 

public class prog7 { 
public static void main(String args[]) 
throws FileNotFoundException 
{ 
Scanner kb=new Scanner(System.in); 

/*here, add code to declare and create a tree map*/ 
TreeMap treeMap = new TreeMap(); 

/*here, add code to declare a variable and 
let it be the key set of the map 
*/ 
String key; 

//temporary variables 
String tempWord; 
String tempDef; 

//the following code reads data from the file glossary.txt 
//and saves the data as entries in the map 
Scanner infile=new Scanner(new File("glossary.txt")); 

while(infile.hasNext()) 
{ 
    tempWord=infile.nextLine(); 
    tempDef=infile.nextLine(); 

    /*here, add code to add tempWord and tempDef 
    as an entry in the map 
    */ 
    treeMap.put(tempWord, tempDef); 

} 
infile.close(); 

while(true) 
{ 
    System.out.println(); 
    System.out.println(); 

    //show menu and prompt message 
    System.out.println("Please select one of the following actions:"); 
    System.out.println("q - Quit"); 
    System.out.println("a - List all words and their definitons"); 
    System.out.println("b - Enter a word to find its definition"); 
    System.out.println("c - Add a new entry"); 
    System.out.println("d - Delete an entry"); 
    System.out.println("Please enter q, a, b, c or d:"); 

    String selection=kb.nextLine(); //read user's selection 
    if (selection.equals("")) continue; //if selection is "", show menu again 

    switch (selection.charAt(0)) 
    { 
    case 'q': 
     System.out.println("\nThank you."); 
     return; 

     /*write code for the cases 'a','b','c' and 'd' 
     so that the program runs as in the sample run 
     */ 

    case 'a': 
     for (String treeKey : treeMap.keySet()) 
      System.out.println(treeKey); 


    break; 
+0

+1使用功课标签和张贴你到目前为止的代码。 – 2011-04-22 15:53:46

回答

1
for (String treeKey : treeMap.keySet()) 

那给你钥匙。

现在在该循环中,使用密钥(treeKey)从treeMap中获取每个值并打印出来。

0

您可以在循环中使用treeMap.get(treeKey)来获取密钥的值。由于该值是一个字符串,你可能只是这样做:在的entrySet而非键集

System.out.println("The value for the current key is " + (String)treeMap.get(treeKey)); 
+0

啊。所以我会以与在搜索后只打印一个相同的方式打印所有这些文件。我懂了。这很明显。有时我想知道当我有这样的时刻时,我为什么是计算机科学专业。 – Lish 2011-04-22 15:55:26

+0

@Lish - 是的,我在最新的编辑中提供了一个示例println,以便您了解如何执行此操作。希望它有帮助。 – dcp 2011-04-22 15:56:04

+0

@Lish - 不客气。祝你好运。 – dcp 2011-04-22 16:02:14

13

迭代。您会得到一组Map.Entry<K, V>,它们有方便的getKey()getValue()方法。

也就是说,Java的标准Map实现有一个toString()的实现,它可以实现你想要的。当然,我想你只会得到点重新实现它,而不是巧妙地避开它...

for (Map.Entry<K, V> entry : myMap.entrySet()) { 
    System.out.println("Key: " + entry.getKey() + ". Value: " + entry.getValue()); 
} 
+0

EntrySet是Set类的一部分吗?我无法在程序中添加任何其他类。 – Lish 2011-04-22 15:58:12

+0

不,它是地图的一部分:http://download.oracle.com/javase/6/docs/api/java/util/TreeMap.html#entrySet%28%29 – 2011-04-22 15:59:27

+0

ahh。有趣。如果我无法找到其他工作,我可能必须尝试。我想尝试和坚持教授期望的尽可能多的东西。尽管如此,感谢您的高举。如果我现在不使用它,我一定会进一步研究它。 – Lish 2011-04-22 16:01:41

0

遍历组键,然后检索每个值比遍历集效率较低的条目getEntries())。在前一种情况下,getValue()将不得不每次执行一次新的查找,而getEntries()可以简单地返回地图的全部内容。

静态分析器(如FindBugs)会在您尝试通过映射键的迭代内检索值时发出警告。

+0

在这种情况下,我需要制作另一种类型的treeMap对象吗?如果我尝试并使用该命令,则会出现以下错误: 'File:Z:\ CS121 \ Project 7 \ prog7.java [line:79] 错误:Z:\ CS121 \ Project 7 \ prog7.java :79:找不到符号 符号:方法getEntries() location:类java.util.TreeMap' – Lish 2011-04-22 16:09:36

+1

对不起。该方法被称为'entrySet'。但说实话,你应该能够自己找到。 Map类的API文档不难找到。 – JesperE 2011-04-22 19:14:24

6

您可以使用entrySet()。 java中的每个Map都有这个方法。

Map<String, String> tree = new TreeMap<String, String>(); 
tree.put("param1", "value1"); 
tree.put("param2", "value2"); 
for (Entry<String, String> entry : tree.entrySet()) { 
    String key = entry.getKey(); 
    String value = entry.getValue(); 

    System.out.printf("%s : %s\n", key, value); 
}