How to convert byte[] to Byte[] and the other way around?

前端 未结 7 706
小鲜肉
小鲜肉 2020-12-02 10:18

How to convert byte[] to Byte[] and also Byte[] to byte[], in the case of not using any 3rd party library?

Is the

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 10:58

    Java 8 solution:

    Byte[] toObjects(byte[] bytesPrim) {
        Byte[] bytes = new Byte[bytesPrim.length];
        Arrays.setAll(bytes, n -> bytesPrim[n]);
        return bytes;
    }
    

    Unfortunately, you can't do this to convert from Byte[] to byte[]. Arrays has setAll for double[], int[], and long[], but not for other primitive types.

提交回复
热议问题