Android 几种拼接数组合并数组的方法

实际项目中需要使用数组拼接合并,还有需要截取。

下面是网上搜集整理的四个方法:

一,apache-commons

二,Arrays.copyOf

三,Array.newInstance

四,System.arraycopy


一,apache-commons

好像是jdk中提供的方法。

我这里打不出来。。。  估计是少下点东西,懒得弄了。。。直接其他三个方法把。


二,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;
    }

下面是合并多个:

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;  
}  

下面是调用:

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 + "---");

这个里面用到了 System.arraycopy() 方法  这个最后说


三,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;
    }

下面是调用:

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

仔细看会发现里面也有 System.arraycopy()   由此可见 最重要的还是

System.arraycopy()方法


四,System.arraycopy

调用:

 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 + "---");

具体方法:

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

第一个参数是你要取的“原材料”, 第二个参数“原材料”的起始位置,

第三个参数是你要放到的“目标” , 第四个参数是“目标”的起始复制位置,

第五个是想要复制的长度。


现在有一个要求:需要把一个byte[] 的第三位开始截取出来

System.arraycopy(output, 3, outCopy, 0, output.length - 3);
根据上面套出来的话应该这么写(以后方便直接拿来用,哈哈哈)


现在又有一个要求:

outCopy  会获取到值  需要把它每次变了就拼接起来

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));

这个是我项目里面现在暂时写的代码:

我先把用来记录的长度变成0,endTime就懒得说了 和这个没太大关系,然后把会变的 outCopy变成空的,

走到while循环, 然后执行System.arraycopy()方法,把原材料的outCopy的第0位开始截取到

目标的btRead,的第length位(第一次是0 就是第0位,后面获取的outCopy的长度,叠加起来。),

然后复制长度是outCopy的长度。


发布了124 篇原创文章 · 获赞 141 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/weixin_36838630/article/details/79794078