如何用java显示哈希表的内容
我实现了一个哈希表,我想显示内容,但我不知道怎么做 这是我的驾驶课:如何用java显示哈希表的内容,java,hashtable,Java,Hashtable,我实现了一个哈希表,我想显示内容,但我不知道怎么做 这是我的驾驶课: package myHashTable; import java.util.InputMismatchException; import java.util.Scanner; public class MyHashDrivergood{ private static String fileName = ""; private static int choice = 0; private int initialC
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.");
}
}
包myHashTable;
导入java.util.InputMismatchException;
导入java.util.Scanner;
公共类MyHashDrivergood{
私有静态字符串fileName=“”;
私有静态int-choice=0;
私有初始容量=0;
私有静态字符串[]testData=null;
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
MyHashTableDataTested=新的MyHashTable
()>;
while(选项!=999){
试一试{
System.out.println(“请输入使用哈希表的选项:(999:
课程结束);;
System.out.println(“读取文件,将数据存储在数组中,然后显示
内容:1);
System.out.println(“读取文件并将数据存储在哈希表中,以及
显示哈希表的内容:2“;
choice=keyboard.nextInt();
keyboard.nextLine();
}捕获(输入不匹配异常e){
System.out.println(“请只输入数字!”);
keyboard.nextLine();
继续;
}
开关(选择){
案例1:
System.out.print(“请输入要测试的文件名:”);
fileName=键盘.next();
testData=新字符串[MyHashTablegood.getFileSize(文件名)];
testData=MyHashTablegood.getData(文件名,
getFileSize(文件名));
对于(int j=0;j
这是我第一次使用哈希表,我不知道我在做什么。你能用它的toString表示吗?从: 返回此哈希表对象在表单中的字符串表示形式 指一组条目,用大括号括起来,用ASCII字符“,”(逗号和空格)分隔
对不起,我上班的时候回答得太快了。我以为你用的是HashMap。如果你想学习如何迭代一个散列映射。。给你 一种方法是遍历地图并打印出值
for (Map.Entry<String, String> entry : yourMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println ("Key: " + key + " Value: " + value);
}
for(Map.Entry:yourMap.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
System.out.println(“键:+Key+”值:+Value);
}
感谢您提供的哈希映射提示。我真的被哈希表问题缠住了。请展示一下MyHashTable的实现。
for (Map.Entry<String, String> entry : yourMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println ("Key: " + key + " Value: " + value);
}