java 如何在java中设置google protobuf重复字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32081493/
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-11-02 19:37:52  来源:igfitidea点击:

How to set google protobuf repeated field in java

javaprotocol-buffers

提问by Angel

Here is my definition

这是我的定义

message point{
  optional float x = 1;
  optional float y = 2;
}
message test{
  repeated field point = 1;
}

In my example.javafile I am trying to create a builder as follows:

在我的example.java文件中,我试图创建一个构建器,如下所示:

for(i = 0; i < somearr.size(); i++)
{
    // I get x and y values from traversing the array 
    float x = getX;
    float y = getY;

    // now I want to set the repeated field point
}

How do I set the repeated field points?

如何设置重复场点?

回答by engineerC

Very similar to repeated PhoneNumberexample here.

非常类似于这里的repeated PhoneNumber示例。

Capitalizing those messages will help code readability.

将这些消息大写将有助于代码可读性。

message Point {
  optional float x = 1;
  optional float y = 2;
}
message Test {
  repeated Point point = 1;
}

java:

爪哇:

Test.Builder b = Test.newBuilder();

for (i = 0; i < somearr.size(); i++) {
    float x = getX; // somehow?
    float y = getY; // ??
    b.addPoint(Point.newBuilder().setX(x).setY(y).build());
}

Test mytest = b.build();

回答by Numuda

List<Point> points = ...;
Test.newBuilder().addAllPoint(points);