简体   繁体   中英

Java - Iterator: “Syntax error, parameterized types are only available if source level is 5.0”

I'm purposely targeting Java OS 1.4.2

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

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

the code is:

Iterator<HSSFRow> myIter = null;

*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();

    • I also have imported the HSSFRow variable

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!

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

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

A more complete example:

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

and the output for this is:

value: a
value: b
value: c

You can't use generics (ie parameterized types ) in Java versions prior to 1.5. Generics where introduced in 1.5 and even the syntax SomeClass<T> is not supported in 1.4.2. That's why you get the error.

Are you sure it's not Iterator<Something> myIter = null; because that seems to be what the error is saying; you're using generics and trying to compile for 1.4, but generics were only added in 1.5.

edit: You can use the key ` (left of 1 key) to delimit code blocks; You need to delete the generics and just use Iterator myIter = null;

Try this :

Iterator rows = sheet.rowIterator();

    while (rows.hasNext())
    {
        row=(HSSFRow) rows.next();
        Iterator cells = row.cellIterator();

        while (cells.hasNext())
        {
            cell=(HSSFCell) cells.next();

            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
            {
                System.out.print(cell.getStringCellValue()+" ");
            }
            else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
            {
                System.out.print(cell.getNumericCellValue()+" ");
            }
            else
            {
                //U Can Handel Boolean, Formula, Errors
            }
        }
        System.out.println();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM