2015-06-11 58 views
-1

我正在創建一個程序,該程序創建一個具有泛型鍵和泛型數據的泛型哈希表。當我將數據數組轉換爲類型Person時,我創建了一個類,並嘗試使用getter方法從我的Driver類訪問存儲在單個Person中的數據,然後返回Object類型。我的問題是,如何通過泛型類訪問非泛型類的信息,因爲它存儲在通用數組中。在泛型類中訪問泛型數組中的值

我這是怎麼建造的散列表:

//region Instance Variables 
    private int count; 
    private K[] keys; 
    private E[] data; 
    private boolean[] hasBeenUsed; 
    //endregion 


    //region Constructor 
    public Table(int capacity) 
    { 
     if (capacity <= 0) 
      throw new IllegalArgumentException("Capacity is negative."); 

     keys = (K[]) new Object[capacity]; 
     data = (E[]) new Object[capacity]; 
     hasBeenUsed = new boolean[capacity]; 
    } 

我的數據干將(該Table類的一部分):

public E[] getData() 
    { 
     return data; 
    } 

    public E getDataAt(int index) 
    { 
     return data[index]; 
    } 

這是我想從我的驅動程序中訪問信息類:

public void print(Table hash) 
    { 
     Person[] people = toArray(hash); 
     for (int i = 0; i < hash.getData().length; i++) 
     { 
      if (null == hash.getKeyAt(i)) 
       System.out.println("NULL AT " + i); 
      else 
       System.out.println("Key: " + hash.getKeyAt(i) + " Data: " + hash.getDataAt(i)); 
     } 
    } 


    private Person[] toArray(Table hash) 
    { 
     Person[] people = new Person[hash.getData().length]; 

     for (int i = 0; i < hash.getData().length; i++) 
     { 
      people[i] = hash.getDataAt(i); 
     } 
    } 

這是我的整個Hashtable類,如果需要這種耐心:

public class Table<K,E> 
{ 
    //region Instance Variables 
    private int count; 
    private K[] keys; 
    private E[] data; 
    private boolean[] hasBeenUsed; 
    //endregion 


    //region Constructors 

    /** 
    * Constructor 
    * Instantiates the keys, data, and hasBeenUsed variables with a passed value of capacity 
    * @param capacity the size to give the three instance arrays 
    */ 
    @SuppressWarnings("unchecked") 
    public Table(int capacity) 
    { 
     if (capacity <= 0) 
      throw new IllegalArgumentException("Capacity is negative."); 

     keys = (K[]) new Object[capacity]; 
     data = (E[]) new Object[capacity]; 
     hasBeenUsed = new boolean[capacity]; 
    } 


    /** 
    * Constructor 
    * Default-Sets arrays to size 10 
    */ 
    @SuppressWarnings("unchecked") 
    public Table() 
    { 
     keys = (K[]) new Object[10]; 
     data = (E[]) new Object[10]; 
     hasBeenUsed = new boolean[10]; 
    } 
    //endregion 


    //region Public Methods 

    /** 
    * Put 
    * Adds a new set to the table 
    * @param key The new Key value 
    * @param data the new Data value 
    * @return null if this is a new set, the old data value if the key already exists 
    */ 
    public E put(K key, E data) 
    { 
     int index = findIndex(key); 
     E answer; 

     if (index != -1) 
     { 
      answer = (E) this.data[index]; 
      this.data[index] = data; 
      return answer; 
     } else if (count < this.data.length) 
     { 
      index = hash(key); 
      while (keys[index] != null) 
      { 
       System.out.println("Collision!"); 
       index = nextIndex(index, key); 
      } 
      keys[index] = key; 
      this.data[index] = data; 
      hasBeenUsed[index] = true; 
      count++; 
      return null; 
     } else 
      System.out.println("ERROR IN PUT"); 
     return null; 
    } 


    /** 
    * Remove 
    * Removes a key-data set from the table 
    * @return the value removed 
    */ 
    @SuppressWarnings("unchecked") 
    public E remove(K key) 
    { 
     int index = findIndex(key); 
     E answer = null; 

     if (index != -1) 
     { 
      answer = (E) data[index]; 
      keys[index] = null; 
      data[index] = null; 
      count--; 
     } 
     return answer; 
    } 


    /** 
    * Contains Key 
    * Checks if the passed key exists 
    * @param key generic type key to check for 
    * @return true if the key exists 
    */ 
    public boolean containsKey(K key) 
    { 
     for (int i = 0; i < data.length; i++) 
     { 
      if (hasBeenUsed[i]) 
      { 
       if (keys[i].equals(key)) 
       { 
        return true; 
       } 
      } 
     } 
     return false; 
    } 


    /** 
    * Get 
    * Retrieves the data held stored with key 
    * @param key the key to access 
    * @return the data at key, null if key does not exist 
    */ 
    @SuppressWarnings("unchecked") 
    public E get(K key) 
    { 
     int index = findIndex(key); 

     if (index == -1) 
      return null; 
     else 
      return (E) data[index]; 
    } 
    //endregion 


    //region Private Methods 

    //Locates the index value of key 
    private int findIndex(K key) 
    { 
     int count = 0; 
     int i = hash(key); 

     while ((count < data.length) && (hasBeenUsed[i])) 
     { 
      if (key.equals(keys[i])) 
       return i; 
      count++; 
      i = nextIndex(i, key); 
     } 

     return -1; 
    } 


    //Hashes the key 
    private int hash(K key) 
    { 
     return Math.abs(key.hashCode()) % data.length; 
    } 


    private int hash2(K key) 
    { 
     return 1 + (Math.abs(key.hashCode()) % (data.length-2)); 
    } 


    //Determines if the next index is valid 
    private int nextIndex(int i, K key) 
    { 
     return (i + hash2(key)) % data.length; 
    } 
    //endregion 


    //region Getters and Setters 
    public int getCount() 
    { 
     return count; 
    } 

    public void setCount(int count) 
    { 
     this.count = count; 
    } 

    public K[] getKeys() 
    { 
     return keys; 
    } 

    public K getKeyAt(int index) 
    { 
     return keys[index]; 
    } 

    public void setKeys(K[] keys) 
    { 
     this.keys = keys; 
    } 

    public E[] getData() 
    { 
     return data; 
    } 

    public E getDataAt(int index) 
    { 
     return data[index]; 
    } 

    public void setData(E[] data) 
    { 
     this.data = data; 
    } 
    //endregion 
} 

編輯 我編輯的打印方法,現在我得到一個ClassCastException

這裏是我的新print方法:

public void print(Table<Integer, Person> hash) 
    { 
     for (int i = 0; i < hash.getKeys().length; i++) 
     { 
      if (null == hash.getKeyAt(i)) 
       System.out.println("NULL AT " + hash.getKeyAt(i)); 
      else 
      { 
       System.out.println("Data at key " + hash.getKeyAt(i) + ": \n"); 
       hash.getDataAt(i).printInfo(); 
      } 
     } 
    } 
+2

實際上,'Table'是通用的。當你說'print(Table hash)'時,你正在使用[raw type](http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html)。不要使用原始類型。 –

回答