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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值