2013-11-21 4 views
1

해시 테이블을 구현했는데 내용을 표시하고 싶지만 어떻게 해야할지 잘 모릅니다.자바에서 내 해시 테이블의 내용을 표시하는 방법

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."); 

} 
} 

그것은 내가 해시 테이블에서 작동 처음 내가 내가 뭐하는 거지의 확실하지 않다 :

여기 내 드라이버 클래스입니다.

+0

을 인쇄하는 것입니다. – Melquiades

답변

2

죄송합니다. 일할 때 너무 빨리 대답했습니다. 당신이 HashMap을 사용하고 있다고 생각했습니다. 그런데 경우에 당신이 여기에 당신이

잘 갈 .. 해시 맵을 반복하는 방법을 배우고 싶다면, 방법은 하나 MyHashTableTable의 구현을 보여주십시오지도를 통해 반복하고 값

for (Map.Entry<String, String> entry : yourMap.entrySet()) { 
    String key = entry.getKey(); 
    String value = entry.getValue(); 

    System.out.println ("Key: " + key + " Value: " + value); 
} 
+0

해시지도 힌트를 보내 주셔서 감사합니다. 난 정말 해시 테이블 문제에 붙어있다. – user2976636

1

그냥 toString 표현을 사용할 수 있습니까? Oracle docs of HashTable에서 :

String toString() 

양식 항목의 집합의 괄호로 둘러싼 ASCII 문자 ","(콤마와 스페이스)로 단락에서 Hashtable 오브젝트의 캐릭터 라인 표현을 돌려줍니다.

관련 문제