英文:
Google Cloud Vision Block Index
问题
以下是翻译好的部分:
能否为我提供有关如何获取正在迭代的块的索引的信息?也就是说,如果我需要在迭代过程中知道处于哪个块编号。
以下是一个示例:
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());
英文:
could you give me information on how to obtain the index of the block that is iterating, that is, if I need to know in what block number during the iteration of this.
Here its an example:
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());
答案1
得分: 2
如果您需要知道索引,请不要使用增强型for循环 - 使用普通for循环,通过索引访问元素。(除非自从我上次查看Java protobuf实现以来发生了显著变化,否则通过索引访问是廉价的。)
for (int i = 0; i < page.getBlocksCount(); i++) {
Block block = page.getBlocksList().get(i);
// ...
}
英文:
If you need to know the index, just don't use the enhanced for loop - use a regular for loop, and access the element by index. (Unless things have changed significantly since I last looked at the Java protobuf implementation, access by index is cheap.)
for (int i = 0; i < page.getBlocksCount(); i++) {
Block block = page.getBlocksList().get(i);
// ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论