Java - Iterator:“语法错误,参数化类型仅在源级别为5.0时才可用” [英] Java - Iterator: "Syntax error, parameterized types are only available if source level is 5.0"

查看:180
本文介绍了Java - Iterator:“语法错误,参数化类型仅在源级别为5.0时才可用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我故意针对Java OS 1.4.2

I'm purposely targeting Java OS 1.4.2

我正在尝试使用迭代器和apache的POI来读取excel电子表格。

I'm trying to use an iterator combined with apache's POI to read an excel spreadsheet.

代码在java 1.5中运行完美,但在1.4.2版本中,我得到问题主题中列出的错误。

The code runs perfectly in java 1.5, but in the 1.4.2 version I get the error listed in the question subject.

代码是:

Iterator<HSSFRow> myIter = null;

*更新 - 删除空声明,并立即将其设置为集合。仍然得到同样的错误! 迭代器无法解析为类型(Iterator是一种抽象类型)。在我尝试从迭代器获取值之前发生此错误!

*Updated - Removed the null declaration, and immediately set it to the collection. Still getting the same error! "Iterator cannot be resolved to a type" (Iterator is an abstract type). This error occurs before I try to get the values from the iterator!

Iterator itRows = hsSheet.rowIterator();



    • 我也导入了HSSFRow变量

    它在那行代码中断,这显然是在应用程序的开头。我不明白为纠正这个问题需要做些什么。如果您有任何见解,请告诉我!

    it breaks on that line of code, which is clearly at the beginning of the application. I dont understand what needs to be done to correct this issue. Please let me know if you have any insight!

    推荐答案

    版本低于1.5的Java不处理泛型类型(也称为其他类型参数化的类型:List)。

    Java with version below 1.5 doesn't handle generic types ( aka types parametrized by other types: List ).

    要在java 1.4中运行代码,您需要松开泛型类型参数并自己进行转换。

    To run your code in java 1.4 you need to loose the generic type parameter and do the casts yourself.

    Iterator myIterator = // initialize it
    HSSFROW row = (HSSFROW)myIterator.next();
    

    更完整的例子:

    List collection = new ArrayList();
    
    collection.add("a");
    collection.add("b");
    collection.add("c");
    
    Iterator myIterator = collection.iterator();
    
    while ( myIterator.hasNext() ) {
        String value = (String) myIterator.next();
        System.out.println("value: " + value);
    }
    

    ,输出为:

    value: a
    value: b
    value: c
    

    这篇关于Java - Iterator:“语法错误,参数化类型仅在源级别为5.0时才可用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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