Google Protobuf 空列表显示大小 1
Google Protobuf Empty List Showing Size 1
我正在使用 Google Protobuf 和 Java。我在发送空的重复字符串时遇到问题。
message OurPokemonMessage {
....
repeated string learnableMoves = 10;
}
现在,当我使用 Protobuf 方法添加一个集合(它是空的)时,如下所示:
message.addAllLearnableMoves(someEmptyListOfString);
现在,当我使用此消息创建一个新的字符串列表时,我在列表中得到一个元素,它是一个空列表。
这就是我使用消息创建字符串列表的方式:
new ArrayList<>(message.getLearnableMovesList())
此数组列表包含一个空字符串元素。所以我调试了消息并评估了 2 个条件,发现了奇怪的结果。
message.getLearnableMovesList()//clearly shows empty list
(com.google.protobuf.UnmodifiableLazyStringList) [] //Empty
message.getLearnableMovesList().size()//shows the size of list is 1
(int) 1 //Size is 1
有没有人遇到过类似的问题?有什么办法可以解决这个问题吗?
所以我后来意识到protobuf库没有问题。问题是在我的 learnableMove 字段中,我添加了一个空字符串,当使用 toString() 打印时显示 [] 而列表的大小为 1.
我正在使用 Google Protobuf 和 Java。我在发送空的重复字符串时遇到问题。
message OurPokemonMessage {
....
repeated string learnableMoves = 10;
}
现在,当我使用 Protobuf 方法添加一个集合(它是空的)时,如下所示:
message.addAllLearnableMoves(someEmptyListOfString);
现在,当我使用此消息创建一个新的字符串列表时,我在列表中得到一个元素,它是一个空列表。
这就是我使用消息创建字符串列表的方式:
new ArrayList<>(message.getLearnableMovesList())
此数组列表包含一个空字符串元素。所以我调试了消息并评估了 2 个条件,发现了奇怪的结果。
message.getLearnableMovesList()//clearly shows empty list
(com.google.protobuf.UnmodifiableLazyStringList) [] //Empty
message.getLearnableMovesList().size()//shows the size of list is 1
(int) 1 //Size is 1
有没有人遇到过类似的问题?有什么办法可以解决这个问题吗?
所以我后来意识到protobuf库没有问题。问题是在我的 learnableMove 字段中,我添加了一个空字符串,当使用 toString() 打印时显示 [] 而列表的大小为 1.