用java实现数组的组合

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数组合并
 * */
public class ArrayCombine {

	/**
	 * 两个字符串数组的合并
	 * */
	public void arrayCombine1(){   //不行
		String[] str1 = {"a","b","e"};
		String[] str2 = {"e","3","w"};
		
		List arr1List = Arrays.asList(str1);
		List arr2List = Arrays.asList(str2);
		
		List list = new ArrayList();
		list.addAll(arr1List);
		list.addAll(arr2List);
		
		Object[] arr3 = list.toArray();
		for(Object obj : arr3){
			System.out.println(obj);
		}
		
	}
	/**
	 * 两个整形数组合并
	 * */
	public void arrayCombine2(){
		int[] arr1 = {1,3,4,6,8};
		int[] arr2 = {23,4,2,14,54};
		int[] arr3 = new int[arr1.length+arr2.length];
		System.arraycopy(arr1, 0, arr3, 0, arr1.length);
		System.arraycopy(arr2, 0, arr3, arr1.length, arr2.length);
	}
	/**
	 * 两个字符串类型数组合并
	 * */
	public void arrayCombine3(){
		String[] str1 = {"a","b","e"};
		String[] str2 = {"e","3","w"};
		String[] str3 = new String[str1.length + str2.length];
		System.arraycopy(str1, 0, str3, 0, str1.length);
		System.arraycopy(str2, 0, str3, str1.length, str2.length);
		for(String s : str3){
			System.out.println(s);
		}
	}
	
	public static void main(String[] args) {
		ArrayCombine ac = new ArrayCombine();
		ac.arrayCombine1();
	}
}

在用Arrays和Collection的addAll方法实现数组的合并的时候要主要对于基本数据类型不能使用这种方式,用完Collection只支持引用数据类型,而用System的arraycopy()方法则是一种通用的解决办法,而且更加灵活。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现n个数组的排列组合,可以使用递归算法。 首先,我们定义一个函数,输入参数为n个数组和一个空的结果列表。函数的作用是将n个数组进行排列组合,并将所有组合的结果保存在结果列表中。 然后,在函数中,我们使用两个循环嵌套来对数组进行排列组合。外层循环用于遍历数组的第一个元素,内层循环用于遍历其余n-1个数组的所有可能组合。 在内层循环中,我们将当前数组的第一个元素与剩余n-1个数组的所有可能组合拼接成新的组合,并递归调用函数。递归调用的参数为剩余的n-1个数组和拼接后的结果列表。 当递归调用的参数数组为空时,表示已经排列组合完成,将结果列表保存到最终的结果集中。 最后,返回最终的结果集。 下面是一个具体实现的示例代码: ```java import java.util.ArrayList; import java.util.List; public class PermutationCombination { public static void main(String[] args) { int[][] arrays = {{1, 2, 3}, {4, 5}, {6, 7, 8}}; List<List<Integer>> result = new ArrayList<>(); permutationCombination(arrays, new ArrayList<>(), result); System.out.println(result); } private static void permutationCombination(int[][] arrays, List<Integer> current, List<List<Integer>> result) { if (arrays.length == 0) { result.add(new ArrayList<>(current)); return; } for (int i = 0; i < arrays[0].length; i++) { current.add(arrays[0][i]); int[][] remaining = new int[arrays.length - 1][]; for (int j = 1; j < arrays.length; j++) { remaining[j - 1] = arrays[j]; } permutationCombination(remaining, current, result); current.remove(current.size() - 1); } } } ``` 运行上述代码,输出结果为: [[1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 5, 6], [1, 5, 7], [1, 5, 8], [2, 4, 6], [2, 4, 7], [2, 4, 8], [2, 5, 6], [2, 5, 7], [2, 5, 8], [3, 4, 6], [3, 4, 7], [3, 4, 8], [3, 5, 6], [3, 5, 7], [3, 5, 8]] 以上代码实现了将3个数组进行排列组合的功能,你可以根据需要修改输入数组的个数来实现任意个数组的排列组合

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值