Is it possible to use the addAll Collection method to add all elements (of type: byte) from arrays (byte []) to a list of type Byte?

I need to add (variable number) byte arrays. Collections seem to work only with wrapper classes, i.e. with a byte. After about 20 hours, I came up with this and it works, but I wondered if it could be improved (ADD TO THE LIST, but any other suggestions for improvement are welcome :), i.e.

Collections.addAll method of all array elements for the specified collection. This calls the Collections.addAll method. This does the same as the Arrays.asList method, however it is much faster than it is so efficient that it is the best way to get the converted array into an ArrayList.

Here is the current beta

public static byte[] joinArrays(byte[]... args) {

    List<Byte> arrayList = new ArrayList(); 

    for (byte[] arg : args) { // For all array arguments...
        for (int i = 0; i < arg.length; i++) { // For all elements in array
            arrayList.add(Byte.valueOf(arg[i])); // ADD TO LIST
        }
    }
    byte[] returnArray = new byte[arrayList.size()]; 

    for (int i = 0; i < arrayList.size(); i++) {
        returnArray[i] = arrayList.get(i).byteValue(); //Get byte from Byte List
    }
    return returnArray;
}
+4
2

/ - :

public static byte[] joinArrays(byte[]... args) {
    int byteCount = 0;
    for (byte[] arg : args) { // For all array arguments...
        byteCount += arg.length;
    }
    byte[] returnArray = new byte[byteCount];
    int offset = 0;
    for (byte[] arg : args) { // For all array arguments...
        System.arraycopy(arg, 0, returnArray, offset, arg.length);
        offset += arg.length;
    }
    return returnArray;
}
+4

public static byte[] joinArrays(byte[]... args) {
    byte[] result = null; 
    if(args.length > 0) {
        result = args[0];
        for(int index = 1; index < args.length; index++){
            result = concat(result, args[index]);
        }       
    }
    return result;
}   

public static byte[] concat(byte[] a, byte[] b) {
    int aLen = a.length;
    int bLen = b.length;
    byte[] c= new byte[aLen+bLen];
    System.arraycopy(a, 0, c, 0, aLen);
    System.arraycopy(b, 0, c, aLen, bLen);
    return c;
}   
0

Source: https://habr.com/ru/post/1599683/


All Articles