`

Java 合并数组

    博客分类:
  • Java
阅读更多

JAVA 合并数组

(1)合并字节数组

/***
	 * 合并字节数组
	 * 
	 * @param a
	 * @return
	 */
	public static byte[] mergeArray(byte[]... a) {
		// 合并完之后数组的总长度
		int index = 0;
		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum = sum + a[i].length;
		}
		byte[] result = new byte[sum];
		for (int i = 0; i < a.length; i++) {
			int lengthOne = a[i].length;
			if (lengthOne == 0) {
				continue;
			}
			// 拷贝数组
			System.arraycopy(a[i], 0, result, index, lengthOne);
			index = index + lengthOne;
		}
		return result;
	}
	/***
	 * append a byte.
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static byte[] appandByte(byte[] a, byte b) {
		int length = a.length;
		byte[] resultBytes = new byte[length + 1];
		System.arraycopy(a, 0, resultBytes, 0, length);
		resultBytes[length] = b;
		return resultBytes;
	}
/***
	 * Get top <code>frontNum</code> bytes
	 * 
	 * @param source
	 * @param frontNum
	 * @return
	 */
	public static byte[] getFrontBytes(byte[] source, int frontNum) {
		byte[] frontBytes = new byte[frontNum];
		System.arraycopy(source, 0, frontBytes, 0, frontNum);
		return frontBytes;
	}

	public static byte[] getAfterBytes(byte[] source, int afterNum) {
		int length = source.length;
		byte[] afterBytes = new byte[afterNum];
		System.arraycopy(source, length - afterNum, afterBytes, 0, afterNum);
		return afterBytes;
	}
/***
	 * 
	 * @param frontNum
	 * @param source
	 * @return
	 */
	public static byte[] filterFrontBytes(int frontNum, byte[] source) {
		return copyByte(frontNum, source.length - frontNum, source);
	}

	public static byte[] copyByte(int start, int length, byte[] source) {
		byte[] des = new byte[length];
		System.arraycopy(source, start, des, 0, length);
		return des;
	}

 

 

(2)java 合并字符串数组

/***
	 * merge two array
	 * @param arr1
	 * @param arr2
	 * @return
	 */
	public static String[] mergeArray2(String[]arr1,String[]arr2){
		int length1=arr1.length;
		int length2=arr2.length;
		int totalLength=length1+length2;
		String[]totalArr=new String[totalLength];
		for(int i=0;i<length1;i++){
			totalArr[i]=arr1[i];
		}
		for(int i=0;i<length2;i++){
			totalArr[i+length1]=arr2[i];
		}
		return totalArr;
	}

 测试:

@Test
	public void test_mergeArray2(){
		String[]str2=new String[]{"111","222"};
		String []input=new String[]{"c","a","baa","c","c1","c"};
		String[] totalArr=SystemHWUtil.mergeArray2(str2, input);
		System.out.println(SystemHWUtil.formatArr(totalArr, " , "));
		System.out.println(totalArr.length);
	}

 运行结果:

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

8

(3)合并int数组

/***
	 * merge two int array to a string
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static String merge(int[] a, int[] b) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < a.length; i++) {
			sb.append(a[i]);
			sb.append(",");
		}
		for (int i = 0; i < b.length; i++) {
			sb.append(b[i]);
			sb.append(",");
		}
		int leng_str = sb.toString().length();
		return sb.substring(0, leng_str - 1);
	}

 

(4)合并char数组

/***
	 * 合并字符数组
	 * 
	 * @param a
	 * @return
	 */
	public static char[] mergeArray(char[]... a) {
		// 合并完之后数组的总长度
		int index = 0;
		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum = sum + a[i].length;
		}
		char[] result = new char[sum];
		for (int i = 0; i < a.length; i++) {
			int lengthOne = a[i].length;
			if (lengthOne == 0) {
				continue;
			}
			// 拷贝数组
			System.arraycopy(a[i], 0, result, index, lengthOne);
			index = index + lengthOne;
		}
		return result;
	}

 

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics