如何在java中设置google protobuf重复字段 [英] How to set google protobuf repeated field in java

查看:30
本文介绍了如何在java中设置google protobuf重复字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的定义

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

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

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
}

如何设置重复场点?

推荐答案

非常类似于 repeated PhoneNumber 示例 此处.

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

这篇关于如何在java中设置google protobuf重复字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆