java byte [] 初始化_【已解决】Android(即Java)中字节数组初始化出错:Cannot define dimension expressions when an array ini...

【问题】

android中想要去初始化byte数组,用如下代码://currentlongAddress = new byte[100]; //[109, -24, 19, -38, 28]

currentlongAddress = new byte[100]{109, -24, 19, -38, 28};

结果出错:Cannot define dimension expressions when an array initializer is provided

如图:

init-byte-array-Cannot-define-dimension-expressions-when-an-array-initializer-is-provided_thumb.png

【解决过程】

1.搜:

android initialize byte array

参考:

貌似我上面写法是对的,但是负数不能直接写,要转换为byte?

2.结果此时才看到,原来错误是:

Cannot define dimension expressions when an array initializer is provided

3.所以,去看看对应的定义,是:private byte[] currentlongAddress;

参考:

把上面100去掉,变成:currentlongAddress = new byte[]{109, -24, 19, -38, 28};

就可以了。

4.但是还是想要实现:

数组长度是100,但是只初始化前面几个数字。

改为:currentlongAddress = new byte[100];

currentlongAddress = {109, -24, 19, -38, 28};

也不行,会出错:Array constants can only be used in initializers

5.貌似只能一个个赋值了???

参考:

也还是没用,貌似只能一点点赋值。

无奈的,最终只能用:currentlongAddress = new byte[100];

//currentlongAddress = {109, -24, 19, -38, 28};

currentlongAddress[0] = 109;

currentlongAddress[1] = -24;

currentlongAddress[2] = 19;

currentlongAddress[3] = -38;

currentlongAddress[4] = 28;

才实现所需要的:

申请一个足够大的,100长度的数组;

然后给前面几个赋值。

【总结】

Java语言,再次证明了语法上,就做得很不好用。

其他的不好用,详见:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值