Java 合并数组

简介:

Java 合并数组

(1)合并字节数组

Java代码   收藏代码
  1. /*** 
  2.      * 合并字节数组 
  3.      *  
  4.      * @param a 
  5.      * @return 
  6.      */  
  7.     public static byte[] mergeArray(byte[]... a) {  
  8.         // 合并完之后数组的总长度  
  9.         int index = 0;  
  10.         int sum = 0;  
  11.         for (int i = 0; i < a.length; i++) {  
  12.             sum = sum + a[i].length;  
  13.         }  
  14.         byte[] result = new byte[sum];  
  15.         for (int i = 0; i < a.length; i++) {  
  16.             int lengthOne = a[i].length;  
  17.             if (lengthOne == 0) {  
  18.                 continue;  
  19.             }  
  20.             // 拷贝数组  
  21.             System.arraycopy(a[i], 0, result, index, lengthOne);  
  22.             index = index + lengthOne;  
  23.         }  
  24.         return result;  
  25.     }  
  26.     /*** 
  27.      * append a byte. 
  28.      *  
  29.      * @param a 
  30.      * @param b 
  31.      * @return 
  32.      */  
  33.     public static byte[] appandByte(byte[] a, byte b) {  
  34.         int length = a.length;  
  35.         byte[] resultBytes = new byte[length + 1];  
  36.         System.arraycopy(a, 0, resultBytes, 0, length);  
  37.         resultBytes[length] = b;  
  38.         return resultBytes;  
  39.     }  
  40. /*** 
  41.      * Get top <code>frontNum</code> bytes 
  42.      *  
  43.      * @param source 
  44.      * @param frontNum 
  45.      * @return 
  46.      */  
  47.     public static byte[] getFrontBytes(byte[] source, int frontNum) {  
  48.         byte[] frontBytes = new byte[frontNum];  
  49.         System.arraycopy(source, 0, frontBytes, 0, frontNum);  
  50.         return frontBytes;  
  51.     }  
  52.   
  53.     public static byte[] getAfterBytes(byte[] source, int afterNum) {  
  54.         int length = source.length;  
  55.         byte[] afterBytes = new byte[afterNum];  
  56.         System.arraycopy(source, length - afterNum, afterBytes, 0, afterNum);  
  57.         return afterBytes;  
  58.     }  
  59. /*** 
  60.      *  
  61.      * @param frontNum 
  62.      * @param source 
  63.      * @return 
  64.      */  
  65.     public static byte[] filterFrontBytes(int frontNum, byte[] source) {  
  66.         return copyByte(frontNum, source.length - frontNum, source);  
  67.     }  
  68.   
  69.     public static byte[] copyByte(int start, int length, byte[] source) {  
  70.         byte[] des = new byte[length];  
  71.         System.arraycopy(source, start, des, 0, length);  
  72.         return des;  
  73.     }  

 

 

(2)java 合并字符串数组

Java代码   收藏代码
  1. /*** 
  2.      * merge two array 
  3.      * @param arr1 
  4.      * @param arr2 
  5.      * @return 
  6.      */  
  7.     public static String[] mergeArray2(String[]arr1,String[]arr2){  
  8.         int length1=arr1.length;  
  9.         int length2=arr2.length;  
  10.         int totalLength=length1+length2;  
  11.         String[]totalArr=new String[totalLength];  
  12.         for(int i=0;i<length1;i++){  
  13.             totalArr[i]=arr1[i];  
  14.         }  
  15.         for(int i=0;i<length2;i++){  
  16.             totalArr[i+length1]=arr2[i];  
  17.         }  
  18.         return totalArr;  
  19.     }  

 测试:

Java代码   收藏代码
  1. @Test  
  2.     public void test_mergeArray2(){  
  3.         String[]str2=new String[]{"111","222"};  
  4.         String []input=new String[]{"c","a","baa","c","c1","c"};  
  5.         String[] totalArr=SystemHWUtil.mergeArray2(str2, input);  
  6.         System.out.println(SystemHWUtil.formatArr(totalArr, " , "));  
  7.         System.out.println(totalArr.length);  
  8.     }  

 运行结果:

111 , 222 , c , a , baa , c , c1 , c

8

(3)合并int数组

Java代码   收藏代码
  1. /*** 
  2.      * merge two int array to a string 
  3.      *  
  4.      * @param a 
  5.      * @param b 
  6.      * @return 
  7.      */  
  8.     public static String merge(int[] a, int[] b) {  
  9.         StringBuffer sb = new StringBuffer();  
  10.         for (int i = 0; i < a.length; i++) {  
  11.             sb.append(a[i]);  
  12.             sb.append(",");  
  13.         }  
  14.         for (int i = 0; i < b.length; i++) {  
  15.             sb.append(b[i]);  
  16.             sb.append(",");  
  17.         }  
  18.         int leng_str = sb.toString().length();  
  19.         return sb.substring(0, leng_str - 1);  
  20.     }  

 

(4)合并char数组

Java代码   收藏代码
  1. /*** 
  2.      * 合并字符数组 
  3.      *  
  4.      * @param a 
  5.      * @return 
  6.      */  
  7.     public static char[] mergeArray(char[]... a) {  
  8.         // 合并完之后数组的总长度  
  9.         int index = 0;  
  10.         int sum = 0;  
  11.         for (int i = 0; i < a.length; i++) {  
  12.             sum = sum + a[i].length;  
  13.         }  
  14.         char[] result = new char[sum];  
  15.         for (int i = 0; i < a.length; i++) {  
  16.             int lengthOne = a[i].length;  
  17.             if (lengthOne == 0) {  
  18.                 continue;  
  19.             }  
  20.             // 拷贝数组  
  21.             System.arraycopy(a[i], 0, result, index, lengthOne);  
  22.             index = index + lengthOne;  
  23.         }  
  24.         return result;  
  25.     }  
相关文章
C4.
|
1月前
|
存储 Java 数据处理
Java的数组
Java的数组
C4.
11 0
|
21天前
|
Java
java 8 数组转字符串并以逗号分隔
java 8 数组转字符串并以逗号分隔
11 0
|
28天前
|
Java
【Java】数组中的拷贝方法与初步理解深浅拷贝
【Java】数组中的拷贝方法与初步理解深浅拷贝
12 0
|
28天前
|
存储 Java C语言
【Java】以数组为例简单理解引用类型变量
【Java】以数组为例简单理解引用类型变量
14 1
|
29天前
|
存储 Java 索引
Java数组
Java数组
7 0
|
30天前
|
Java
java中判断数组中元素出现的次数
java中判断数组中元素出现的次数
9 0
|
30天前
|
Java
java向数组中插入元素
java向数组中插入元素
9 0
|
1月前
|
存储 Java 索引
JAVA一维数组
JAVA一维数组
17 3
|
1月前
|
Java 索引
JAVA数组的常用方法
JAVA数组的常用方法
16 1
|
1月前
|
Java C语言
Java中的数组,你知道多少细节?
Java中的数组,你知道多少细节?
24 1