我如何在Java中显示哈希表的内容 [英] How I display the content of my hash table in java

查看:109
本文介绍了我如何在Java中显示哈希表的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个哈希表,我想显示其内容,但是我不知道该怎么做.

I implemented a hash table and I want to display the contents, but I do not know how to do this.

这是我的驱动程序类:

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.

推荐答案

抱歉,当我开始工作时,回答得太快了.我以为您正在使用HashMap.好吧,以防万一,您想学习如何迭代哈希图...

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

好吧,一种方法是遍历地图并打印出值

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);
}

这篇关于我如何在Java中显示哈希表的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆