int[] outSize = new int[]{bytes, packets};
new byte[] {0, 0, 0, 0}
第一行代码是什么意思?它对大小数组做了什么? 第二行代码如何初始化该字节数组(如果它完全是这样的话)?
答案 0 :(得分:0)
我们可以使用给定的syntex
在声明时初始化一个数组int[] outSize = new int[]{5, 9}; //create a Integer array of 2 element named outSize
OR
int bytes = 5, packets =9;
int[] outSize = new int[]{bytes, packets}; //create a Integer array of 2 element named outSize.
使用上述两种说法没有区别。
答案 1 :(得分:0)
第一行
int[] outSize = new int{ bytes, packets };
这将创建一个包含两个整数元素的整数(基元)数组,即bytes
和packets
。
第二行
new byte[] { 0, 0, 0, 0 }
本身,这会给你一个错误(不是编译),因为它不是一个语句(在任何地方都没有赋值给变量)。
如果你写
byte[] bytes = new byte[] { 0, 0, 0, 0 };
您正在声明一个名为bytes
的字节数组,其中包含四个元素。
使用{}进行数组初始化
请参阅documentation的第10章(数组),特别是第10.1和10.2节。
另外值得一读的是primitive data types。
从上面开始:
int
是一个32位带符号的二进制补码整数(基数为10的最小值为-2^31
,最大值为2^31 - 1
)。
byte
是一个8位带符号的二进制补码整数(基数为10的最小值为-2^7
,最大值为-2^7 - 1
)。
正在做什么
第一行使用64位来存储两个整数。
第二行使用32位来存储四个字节。