How to set google protobuf repeated field in java

左心房为你撑大大i 提交于 2019-12-12 06:19:33

问题


Here is my definition

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

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

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?


回答1:


Very similar to repeated PhoneNumber example here.

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



回答2:


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


来源:https://stackoverflow.com/questions/32081493/how-to-set-google-protobuf-repeated-field-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!