[英]Length of byte[] array
String str = "123456789";
byte[] bytes = str.getBytes();
I want to make the following loop 我想做以下循环
for (int j = 0; j < bytes.length(); j++) {
b = bytes[j];
}
b
will store each byte of my array, but I can't seem to get the length of the array correctly. b
将存储我的数组的每个字节,但我似乎无法正确获取数组的长度。
Error:cannot find symbol
Problem solved: bytes.length instead of bytes.length()
使用bytes.length
而不使用()
See the JLS - 10.7. 参见JLS - 10.7。 Array Members :
数组成员 :
The members of an array type are all of the following:
数组类型的成员是以下所有成员:
The
public final field length
, which contains the number of components of the array.public final field length
,包含数组的组件数。 length may be positive or zero.长度可以是正数或零。
length
is a property , not a method. length
是属性 ,而不是方法。 You should write: 你应该写:
bytes.length
它不是bytes.length()
,它的bytes.length
In order to get the length of an array, use length
. 为了获得数组的长度,请使用
length
。 For example: 例如:
int[] arr = {1,2,3};
int length = arr.length;
In order to get the length of a string, use length()
. 为了获得字符串的长度,请使用
length()
。 For example: 例如:
String str = "123";
int length = str.length();
getBytes() Encodes the String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. getBytes()使用平台的默认字符集将String编码为字节序列,将结果存储到新的字节数组中。
Try to print the bytes you will see the encoded values which may differ in length compare to original string. 尝试打印字节,您将看到编码值,这些编码值的长度可能与原始字符串不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.