通过Int变量表示空字节数组?

Raj*_*pta 30 java variables bytearray java-ee

应该为整数类型变量分配什么值来表示空字节数组?我需要这个在数据库中存储一个空字节数组,以表示Cassandra中的无值列.

Moh*_*our 81

根据Cassandra API http://wiki.apache.org/cassandra/API一个空字节数组是

byte[] emptyArray = new byte[0];
Run Code Online (Sandbox Code Playgroud)


Ste*_*n C 8

在一般的Java术语中,空字节数组是长度为零的字节数组,可以使用Java表达式创建new byte[0].根据接受的答案,Casandra API使用标准术语.

您不能*使用整数类型变量表示字节数组,因为没有合理的方法来表示单个整数中字节数组的内容和长度.

* - 实际上你可以编码一个小字节数组(最长为3)的状态int,但编码/解码会很乱......这与OP的问题无关.


Abh*_*eet 6

在java中,您可以通过提供所需数组的长度来创建一个新数组

new type[length]

private byte[] getByteResponse() {
    return new byte[0];
}
Run Code Online (Sandbox Code Playgroud)

这个应该可以。