groovy中的字节数组

hud*_*udi 29 java groovy bytearray

在java中我可以创建字节数组:byte[] array = new byte[] { 0, 0, 0, 0, 0 }; 但是这个结构在groovy中是无效的.如何在groovy中创建字节数组?

Ric*_*lly 41

以下应该足够了:

def array = [0, 0, 0, 0, 0] as byte[]
Run Code Online (Sandbox Code Playgroud)

看看这里为在常规阵列中更多的细节.


tim*_*tes 15

除了rich.okelly的回答,

byte[] array = [0, 0, 0, 0, 0]
Run Code Online (Sandbox Code Playgroud)

也有效


smc*_*mcg 5

您不能以相同的方式初始化文字数组,因为Groovy认为大括号形成了闭包。你想要的是像

def x = [ 0, 0, 0, 0, 0 ] as byte[]
Run Code Online (Sandbox Code Playgroud)

查看更多:这里