有没有一种简单的方法可以在不循环的情况下检查Java中的字节数组是否所有值都为0xFF?
例子:
例子:
byte[] b = new byte[]{ 0xff, 0xff, 0xff, 0xff, 0xff };
if (b is all 'ff')
process?
byte[] b = new byte[]{ 0xff, 0xff, 0xff, 0xff, 0xff };
if (b is all 'ff')
process?
public static void test1() {
class Chk {
boolean c(int [] b, int val, int pos) {
if (pos >= b.length) {
return true;
}
if (b[pos] != val) {
return false;
}
return c(b, val, pos + 1);
}
}
Chk test = new Chk();
System.out.println(test.c(new int [] {0xff, 0xff}, 0xff, 0));
System.out.println(test.c(new int [] {0xff, 0xff, 0xff, 0xfe}, 0xff, 0));
System.out.println(test.c(new int [] {0x01, 0x01, 0x01, 0x01}, 0xff, 0));
System.out.println(test.c(new int [] {0x01, 0x01, 0x01, 0x01}, 0x01, 0));
}
疯狂的想法,你可以用字符串匹配实现它
int[] b = new int[]{0xff, 0xff, 0xff, 0xff, 0xff};
String arr = Arrays.toString(b).replaceAll(", ", "");
String match = "\\[("+new Integer(0xff).toString()+")+\\]";
System.out.println(arr);
System.out.println(match);
System.out.print(arr.matches(match));