Several Android splicing arrays into an array of methods

The actual project requires the use of an array of stitching combined, there needs to be extracted.

Here are four methods of collection and collation online:

A, apache-commons

二,Arrays.copyOf

三,Array.newInstance

四,System.arraycopy


A, apache-commons

Jdk method seems to be provided.

I do not come out here to play. . . It is estimated that at least something, too lazy to get out. . . The other three direct methods.


二,ArrayscopyOf()

 public static <T> byte[] concat(byte[] first, byte[] second) {
        byte[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

The following are merging multiple:

public static <T> T[] concatAll(T[] first, T[]... rest) {  
  int totalLength = first.length;  
  for (T[] array : rest) {  
    totalLength += array.length;  
  }  
  T[] result = Arrays.copyOf(first, totalLength);  
  int offset = first.length;  
  for (T[] array : rest) {  
    System.arraycopy(array, 0, result, offset, array.length);  
    offset += array.length;  
  }  
  return result;  
}  

Here is the call:

private byte[] aaa;
    private byte[] bbb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        aaa = new byte[5];
        bbb = new byte[5];
 byte[] ccc = concat(aaa, bbb);
        LogUtil.fussenLog().d(DensityUtils.bytesToHexString(ccc));
        LogUtil.fussenLog().d(ccc.length + "---");

This used inside System.arraycopy () method that concludes


三,Array.newInstance

private static <T> byte[] concat2(byte[] a, byte[] b) {
        final int alen = a.length;
        final int blen = b.length;
        if (alen == 0) {
            return b;
        }
        if (blen == 0) {
            return a;
        }
        byte[] result = (byte[]) java.lang.reflect.Array.
                newInstance(a.getClass().getComponentType(), alen + blen);
        System.arraycopy(a, 0, result, 0, alen);
        System.arraycopy(b, 0, result, alen, blen);
        return result;
    }

Here is the call:

byte[] ccc = concat2(aaa, bbb);
        LogUtil.fussenLog().d(DensityUtils.bytesToHexString(ccc));
        LogUtil.fussenLog().d(ccc.length + "---");

A closer look will find that there is also a System.arraycopy () shows that the most important thing

System.arraycopy () method


四,System.arraycopy

transfer:

 byte[] ccc = new byte[aaa.length + bbb.length];
        System.arraycopy(aaa, 0, ccc, 0, aaa.length);
        System.arraycopy(bbb, 0, ccc, aaa.length, bbb.length);
        LogUtil.fussenLog().d(DensityUtils.bytesToHexString(ccc));
        LogUtil.fussenLog().d(ccc.length + "---");

specific method:

System.copy(Object src , int srcPos , Object dest , int destPost , int length);

The first argument is that you should take the "raw material", the second argument "raw material" starting position,

The third argument is that you should put "target", the fourth parameter is the "target" of the copy start position,

The fifth is the length you want to copy.


There is now a requirement: the need for a byte [] is taken out of the third start

System.arraycopy(output, 3, outCopy, 0, output.length - 3);
According to the words set out above would look like this (to facilitate future directly used, ha ha ha)


Now I have one request:

outCopy will get to value every time it needs to be changed on stitching together

length = 0;
        endTime = maxWaitTime + System.currentTimeMillis();
        outCopy = null;
        while (endTime > System.currentTimeMillis()) {
            if (outCopy != null) {
                System.arraycopy(outCopy, 0, btRead, length, outCopy.length);
                length = length + outCopy.length;
                LogUtil.fussenLog().d("单次接收:" + DensityUtils.bytesToHexString(outCopy));
                outCopy = null;
                endTime = 50 + System.currentTimeMillis();
            }
        }
        LogUtil.fussenLog().d("接收:" + DensityUtils.bytesToHexString(btRead));
        btReal = new byte[length];
        System.arraycopy(btRead, 0, btReal, 0, length);
        LogUtil.fussenLog().d("接收转换:" + DensityUtils.bytesToHexString(btReal));

This is my project which is now being written in the code:

I first used to record the length becomes 0, endTime too lazy to say this and not much relationship, then will become outCopy become empty,

Go while loop, then perform System.arraycopy () method, the raw materials of outCopy bit to zero intercept

Target btRead, the bit length of (the first bit 0 is 0, the length of the acquired back outCopy, add up.),

Then copy length is a length of outCopy.


Published 124 original articles · won praise 141 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_36838630/article/details/79794078