Comment puis-je imprimer toutes les valeurs dans un TreeMap?

J'ai un projet sur lequel je travaille pour ma classe Java (évidemment) et je dois avoir manqué la conférence sur la façon d'interagir avec les Arborescences. Je n'ai aucune idée de ce que je suis en train de faire avec cette partie et je n'ai pas trouver beaucoup d'aide de Google.

Pour le premier cas dans le programme, j'ai l'impression de toutes les valeurs d'une TreeMap. Voici le code que j'ai été fournis et le travail que j'ai fait avec elle. Tout dans le cas où A est la mienne, mais il ne fonctionne pas. Toute aide serait appréciée.

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;
+1 pour l'aide aux devoirs et la balise de poster le code que vous avez jusqu'à présent.

OriginalL'auteur Lish | 2011-04-22