2014-12-02 38 views
0

我在这个代码块具有标识符预期的错误:标识符预期误差哈希表

@SuppressWarnings({"rawtypes","unchecked"}) 
    theLists = new List<AnyType>[ nextPrime(2 * theLists.length) ]; 
for(int j = 0; j < theLists.length; j++) 
     theLists[ j ] = new LinkedList<>(); 

编译器说后“theLists”和=前应该有一个标识符。什么标识符?我该如何解决?

这是完整的方法:

 private void rehash() 
{ 
    List<AnyType> [ ] oldLists = theLists; 

     // Create new double-sized, empty table 
@SuppressWarnings({"rawtypes","unchecked"}) 
    theLists = new List<AnyType>[ nextPrime(2 * theLists.length) ]; 
for(int j = 0; j < theLists.length; j++) 
     theLists[ j ] = new LinkedList<>(); 

     // Copy table over 
    currentSize = 0; 
    for(List<AnyType> list : oldLists) 
     for(AnyType item : list) 
      insert(item); 
} 

我删除从身体的禁止警告,只是把它的构造,但它仍说标识符预期:

public HashTable(int size) 
{ 

    @SuppressWarnings("unchecked") theLists = (List<AnyType>[]) new List<?>[nextPrime(2 * theLists.length)]; 
for(int i = 0; i < theLists.length; i++) 
     theLists[ i ] = new ArrayList<>(); 

} 
+1

不能使用非数组类型括号标记。另外,'List'不能被实例化。 – August 2014-12-02 01:32:39

+0

@八月这看起来像一个数组类型。 – 2014-12-02 01:35:14

+0

这是一个数组类型。 – user2820406 2014-12-02 01:37:15

回答

0

的注释可以只出现在following locations

  • 我的ThOD声明(包括注释类型的元素)
  • 构造函数声明
  • 字段声明(包括枚举常数)
  • 形式和异常参数声明
  • 本地变量声明(包括用于 语句循环变量和资源变量尝试与 - 资源语句)

你想在这里应用它

@SuppressWarnings({"rawtypes","unchecked"}) 
theLists = new List<AnyType>[ nextPrime(2 * theLists.length) ]; 

这不是一个声明,而是一个赋值表达式。这是行不通的。而是注释包含此代码的方法。

一旦你这样做,你会得到一个错误,你不能创建一个通用的数组。有关于此的many问题和答案。阅读这些。

你可以做

theLists = (List<AnyType>[]) new List<?>[nextPrime(2 * theLists.length)]; 
+0

我仍然得到一个标识符预期的错误。我使用更新编辑了原始问题。 @Sotirios – user2820406 2014-12-02 02:26:43

+0

@ user2820406您仍然在不可接受的位置使用注释。注释构造函数声明,而不是赋值表达式。 – 2014-12-02 02:27:57

+0

我明白了!谢谢! – user2820406 2014-12-02 03:17:21