错误:只能迭代数组或 java.lang.Iterable 的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22425277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:38:55  来源:igfitidea点击:

Error: can only iterate over an array or an instance of java.lang.Iterable

javaarraysfor-loop

提问by Rohan21

please help me with my error can't seem to make it work because of that can only iterate over an array or an instance of java.lang.Iterable. i want to create a barcode and read it and add it to the word document

请帮助我解决我的错误似乎无法使其工作,因为它只能迭代数组或 java.lang.Iterable 的实例。我想创建一个条形码并读取它并将其添加到word文档中

Update Postthe nodeCollection is from the com.aspose.words.

更新发布nodeCollection 来自 com.aspose.words。

import com.aspose.barcode.*;
import com.aspose.barcoderecognition.BarCodeReadType;
import com.aspose.barcoderecognition.BarCodeReader;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImageType;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.Shape;

              try
        {
            // Generate barcode image
            BarCodeBuilder builder = new BarCodeBuilder();
            builder.setSymbologyType(Symbology.Code39Standard);
            builder.setCodeText("test-123");
            String strBarCodeImageSave = "img.jpg";
            builder.save(strBarCodeImageSave);

            // Add the image to a Word doc
            Document doc = new Document();
            DocumentBuilder docBuilder = new DocumentBuilder(doc);
            docBuilder.insertImage(strBarCodeImageSave);
            String strWordFile = "docout.doc";
            doc.save(strWordFile);

            // Recognition part
            // Extract image from the Word document
            NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
            int imageIndex = 0;

            for(Shape shape: shapes)
            {   
                if (shape.hasImage())
                {
                    // If this shape is an image, extract image to file
                    String extension = ImageTypeToExtension(shape.getImageData().getImageType());
                    String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
                    String strBarCodeImageExtracted = "" + imageFileName;
                    shape.getImageData().save(strBarCodeImageExtracted);

                    // Recognize barcode from this image
                    BarCodeReader reader = new BarCodeReader((BufferedImage) Toolkit.getDefaultToolkit().getImage(strBarCodeImageExtracted),BarCodeReadType.Code39Standard);
                    while (reader.read())
                    {
                        System.out.println("codetext: " + reader.getCodeText());
                    }
                    imageIndex++;
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }

    private static String ImageTypeToExtension(int imageType) throws Exception
    {
        switch (imageType)
        {
            case ImageType.BMP:
                return "bmp";
            case ImageType.EMF:
                return "emf";
            case ImageType.JPEG:
                return "jpeg";
            case ImageType.PICT:
                return "pict";
            case ImageType.PNG:
                return "png";
            case ImageType.WMF:
                return "wmf";
            default:
                throw new Exception("Unknown image type.");
        }
    }}

采纳答案by jjavier

I assume Nodecollection is a com.aspose.words.NodeCollection.

我假设 Nodecollection 是一个 com.aspose.words.NodeCollection。

If you want to use the foreach syntax you better do:

如果你想使用 foreach 语法,你最好这样做:

Node[] shapesArray = shapes.toArray();
for (Node node : shapesArray ){ ...

回答by Ravinder Reddy

Error: can only iterate over an array or an instance of java.lang.Iterable

错误:只能迭代数组或 java.lang.Iterable 的实例

It clearly says that you should iterate only on objects which are iterable.

它清楚地表明您应该只对可迭代的对象进行迭代。

In your code you are using

在您使用的代码中

NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
...    
for(Shape shape: shapes)

The forloop fails unless the shapesbase class is an instance of a java.util.Collectionor java.lang.Iterable.

for,除非环路出故障时shapes基类是的一个实例java.util.Collectionjava.lang.Iterable

Check if NodeCollectionis a collectiontype class that implemented java.lang.Iterable.

检查是否NodeCollection是一个集合类型的类实现java.lang.Iterable



Edit:

编辑

the nodeCollection is from the com.aspose.words.

nodeCollection 来自 com.aspose.words。

NodeCollectionimplements generic Iterabledirectly, without specifying the type of objects it would be handling. Hence you should explicitly generate the Iteratorfrom the NodeCollectioninstance and on that you can iterate.

NodeCollectionIterable直接实现泛型,而不指定它将处理的对象类型。因此,您应该IteratorNodeCollection实例显式生成并且您可以对其进行迭代。

NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
Iterator<Shape> shapesIterator = shapes.iterator();
...    
// now use the above iterator in for loop, as below
for( Shape shape: shapesIterator )

Refer toa similar answer on so

参考so上的类似回答