简体   繁体   中英

How to set google protobuf repeated field in java

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?

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();
List<Point> points = ...;
Test.newBuilder().addAllPoint(points);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM