我如何在java中显示我的哈希表的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20114574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I display the content of my hash table in java
提问by user2976636
I implemented a hash table and I want to display the contents, but I do not know how to do this.
我实现了一个哈希表,我想显示内容,但我不知道该怎么做。
Here is my driver class:
这是我的驱动程序类:
package myHashTable;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MyHashDrivergood{
private static String fileName = "";
private static int choice = 0;
private int initialCapacity = 0;
private static String[] testData = null;
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
MyHashTableTable<String, String> dataTested = new MyHashTableTable<String,String>
()>;
while( choice != 999 ){
try {
System.out.println("Please enter your choice to use the HashTable: (999:
End of program) ");
System.out.println("Read the files, store data in an array, and display
the content: 1 ");
System.out.println("Read the files and store the data in the HashTable, and
dislay the content of the hashtable: 2");
choice = keyboard.nextInt();
keyboard.nextLine();
} catch (InputMismatchException e) {
System.out.println("Please enter only numbers!");
keyboard.nextLine();
continue;
}
switch(choice){
case 1:
System.out.print( "Please enter the file name to test:" );
fileName = keyboard.next();
testData = new String[MyHashTablegood.getFileSize( fileName )];
testData = MyHashTablegood.getData( fileName,
MyHashTablegood.getFileSize( fileName ));
for( int j = 0;j < testData.length; j++ ){
System.out.println( j + " " + testData[j] );
}
break;
case 2:
System.out.print( "Please enter the file name to test:" );
fileName = keyboard.next();
//here I read the data from a file
testData = new String[MyHashTablegood.getFileSize( fileName )];
testData = MyHashTablegood.getData( fileName,
MyHashTablegood.getFileSize( fileName ));
//now I am storing the data in my hash table
for(int k =0; k < testData.length; k++){
String data = testData[k];
dataTested.put(data, data);
}
break;
}
}
keyboard.close();
System.out.println("The program has ended.");
}
}
It is the first time that I work with hash tables and I am not sure of what I am doing.
这是我第一次使用哈希表,我不确定我在做什么。
采纳答案by Shaun
Sorry answered this too quickly when I got to work. I thought you were using a HashMap. Well in case you want to learn how to iterate a hash map.. here you go
抱歉,我上班时回答得太快了。我以为你在使用 HashMap。好吧,如果您想学习如何迭代哈希映射.. 在这里
Well, one way is to iterate through your map and print out the values
嗯,一种方法是遍历您的地图并打印出值
for (Map.Entry<String, String> entry : yourMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println ("Key: " + key + " Value: " + value);
}
回答by bcorso
Can you just use it's toString representation? From the Oracle docs of HashTable:
你能用它的 toString 表示吗?来自HashTable的Oracle 文档:
String toString()
Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
以一组条目的形式返回此 Hashtable 对象的字符串表示形式,用大括号括起来并用 ASCII 字符“,”(逗号和空格)分隔。