问题描述
能给我有关如何获取要迭代的块的索引的信息吗,也就是说,如果我需要知道迭代过程中哪个块号。
这里是一个例子:
TextAnnotation annotation = res.getFullTextAnnotation();
for (com.google.cloud.vision.v1.Page page : annotation.getPagesList()) {
String pageText = "";
System.out.println("Bloque-->" + page.getBlocksCount());
for (Block block : page.getBlocksList()) {
String blockText = "";
//System.out.println("Index of block-->" + block.getIndex());
System.out.println("Paragrafos-->" + block.getParagraphsCount());
解决方法
如果您需要了解索引,就不要使用增强的for循环-使用常规的for循环,并按索引访问元素。 (除非自从我上次查看Java protobuf实现以来情况发生了很大变化,否则通过索引进行访问很便宜。)
for (int i = 0; i < page.getBlocksCount(); i++) {
Block block = page.getBlocksList().get(i);
// ...
}