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

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

一,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;@Overrideprotected 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的长度。

Android 几种拼接数组合并数组的方法相关推荐

  1. php 二维数组字段合并,PHP将二维数组某一个字段相同的数组合并起来的方法,二维数组数组_PHP教程...

    PHP将二维数组某一个字段相同的数组合并起来的方法,二维数组数组 本文实例讲述了PHP将二维数组某一个字段相同的数组合并起来的方法.分享给大家供大家参考,具体如下: 例子: array(3) { [0 ...

  2. 那些方式可以合并php数组,合并数组(PHP)

    如何以这种方式组合数组? 资源: Array ( [0] => Array ( [id] => 3 [title] => book [tval] => 10000 ) [1] ...

  3. java中数组合并的方法,数组合并--Java原生方法

    废话不多说,直接上代码(工具类): public static Object[] combineArray(Object one[], Object two[]) throws BussinessEx ...

  4. 遍历多个数组 合并数组

    foreach ($resData['data'] as $key => $value) {if($value['type']==1){$xtitle[0][] = "热卖" ...

  5. C语言题解 | 去重数组合并数组

    -

  6. android 8种对话框(Dialog)使用方法汇总

    本文为作者原创,转载请注明出处:http://www.cnblogs.com/gzdaijie/p/5222191.html 目录 1.写在前面 2.代码示例 2.1 普通Dialog(图1与图2) ...

  7. Android 两种制作圆形/圆角图片的方法

    前言: 目前网上有很多圆角图片的实例,Github上也有一些成熟的项目.之前做项目,为了稳定高效都是选用Github上的项目直接用.但这种结束也是Android开发必备技能 ,所以今天就来简单研究一下 ...

  8. Android --- 两种设置字体加粗的方法

    直接上代码 tv_subject_name.getPaint().setFakeBoldText(true); tv_subject_name.getPaint().setTypeface(Typef ...

  9. scala 数组合并_Scala程序合并两个数组或数组缓冲区

    scala 数组合并 Scala | 合并两个数组 (Scala | Merging two arrays) Arrays are important data structures in progr ...

最新文章

  1. 分布式存储系统的关键技术
  2. SAP freelancer如何找到一个不苦逼的项目?
  3. 如何查看oracle 查版本号
  4. ubuntu16.04下面xfce4没有声音
  5. Android之实现首尾带圆角的多颜色水平条
  6. linux编码 form表单,Linux以form表单形式上传文件讲解
  7. arm-linux-gcc libstdc .so.6,mini2440编译内核:usr/lib/libstdc++.so.6 not found
  8. linux下Epoll实现简单的C/S通信
  9. php protobuf 二进制,PHP环境中使用ProtoBuf数据格式
  10. windows xp 下使用 windows 2003的远程桌面
  11. 并发编程中,你加的锁未必安全
  12. android 图片加载方式
  13. redis 下载安装 python 操作redis django 连接redis
  14. Fiji-imageJ 无法打开
  15. 数据分析|WordCloud PCA K-means - 「某电商平台」电脑评论分析
  16. Scrapy创建项目报错Scrapy – no active project,Unknown command解决办法
  17. 【ARM编程】ARM介绍
  18. centos7(命令行版)安装teamviewer记录
  19. c语言漩涡图形,AI教程│变换工具实例运用 轻松制作圆形漩涡图案
  20. 所以,你知道你为什么要读书了吗?

热门文章

  1. Unity3D 如何复制内容到剪切板
  2. 什么是pyc文件,把python的py文件编译成pyc文件,把pyc文件反编译成py文件。以及python编译的如何设置不生成pyc文件
  3. 登录Linux时/etc/profile、~/.bash_profile等几个文件的执行过程
  4. MPC 101:安全多方计算与多方签名
  5. 再见,360浏览器!我用60行Python代码制作一款浏览器!
  6. 【计算机视觉】相机模型立体视觉
  7. python+redis_redis操作 + python连接redis(StrictRedis)
  8. 数字未来:世界正走向新的“破茧时刻”
  9. 教你如何给移动硬盘分区
  10. 利用IPV6搭建一个家庭服务器