检查字节数组是否都在0xff中

jan*_*tox 8 java arrays byte

是否有一种简单的方法可以在不循环的情况下检查java中的字节数是否全部为0xFF作为值?

byte[] b = new byte[]{ 0xff, 0xff, 0xff, 0xff, 0xff };

if (b is all 'ff')
    process?
Run Code Online (Sandbox Code Playgroud)

DRC*_*RCB 3

如果您不喜欢循环,请使用递归:)

 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));
}
Run Code Online (Sandbox Code Playgroud)