多个数组元素的组合

时间:2014-03-25 11:12:16

标签: java arrays arraylist

最近我实现了以下代码 假设我有2个数组 -

arr1 = [a,b,c]

arr2 = [d,e]

应该是

output = [ad,ae,bd,be,cd,ce]

现在假设我有多个阵列。

例如:

arr1=[a,b,c]

arr2=[d,e]

arr3=[f,g,h,i]

arrN=[x,y,z,h,o]

output = [adf..x,adf..y and so on]

如何在JAVA中实现这一点?请帮助

我的尝试:

for(int i=0;i<arr1.length();i++)
{
for(int j=0;i<arr2.length();j++)
{
System.out.print(arr1[i] + arr2[j] );
}
}

7 个答案:

答案 0 :(得分:3)

这是我尝试从多个数组(容器)中获取组合。

 private List<List<T>> getCombination(int currentIndex, List<TempContainer<T>> containers) {
    if (currentIndex == containers.size()) {
        // Skip the items for the last container
        List<List<T>> combinations = new ArrayList<List<T>>();
        combinations.add(new ArrayList<T>());
        return combinations;
    }
    List<List<T>> combinations = new ArrayList<List<T>>();
    TempContainer<T> container = containers.get(currentIndex);
    List<T> containerItemList = container.getItems();
    // Get combination from next index
    List<List<T>> suffixList = getCombination(currentIndex + 1, containers);
    int size = containerItemList.size();
    for (int ii = 0; ii < size; ii++) {
        T containerItem = containerItemList.get(ii);
        if (suffixList != null) {
            for (List<T> suffix : suffixList) {
                List<T> nextCombination = new ArrayList<T>();
                nextCombination.add(containerItem);
                nextCombination.addAll(suffix);
                combinations.add(nextCombination);
            }
        }
    }
    return combinations;
}

该方法获取带有项目的容器列表。例如容器1将具有[a,b],而容器2将具有[c,d,e]。该方法可以采用任意数量的容器。     //容器类声明如下:

public class TempContainer<T> {
    private List<T> items; 
    public void setItems(List<T> items) {
       this.items = items;
    }

    public List<T> getItems() {
         return items;
    }
}

按如下方式调用此方法:

List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
List<String> list2 = new ArrayList<String>();
list2.add("3");
list2.add("4");
list2.add("5");
TempContainer container1 = new TempContainer();
container1.setItems(list1);
TempContainer container2 = new TempContainer();
container2.setItems(list2);
List<TempContainer> containers = new ArrayList<TempContainer>(2);
containers.add(container1);
containers.add(container2);
// Get combinations 
List<List<T>> combinations = getCombination(0, containers);

答案 1 :(得分:1)

将数组放在数组中怎么样:

    String[] arr1 = { "a", "b", "c" };

    String[] arr2 = { "d", "e" };

    String[] arr3 = { "f", "g", "h", "i" };

    String[][] arrays = new String[][] { arr1, arr2, arr3 };

    public void mix(String[][] arrays) {

        if (arrays.length > 0) {
            List<String> result = Arrays.asList(arrays[0]);

            for (int i = 1; i < arrays.length - 1; i++) {
            String[] aux = arrays[i];
            for (int j = 0; j < aux.length; j++) {
                result.add(j, result.get(j) + aux[j]);
            }
            }
        }
    }

答案 2 :(得分:1)

我有类似的要求,但是对于不同类型的集合。这是Priyesh解决方案的修改版本。

注意:我删除了对TempContainer<T>类的依赖。

public class TestCombinations {

@Test
public void test() {

    final List<Object> l1 = new ArrayList<Object>();
    l1.add(1);
    l1.add(2);
    final List<Object> l2 = new ArrayList<Object>();
    l2.add("3");
    l2.add("4");
    l2.add("5");
    final List<Object> l3 = new ArrayList<Object>();
    l3.add(true);
    l3.add(false);

    final List<List<Object>> c = new ArrayList<>(3);
    c.add(l1);
    c.add(l2);
    c.add(l3);

    // Get combinations
    final List<List<Object>> m = this.combine(c);
    System.out.println(m);

}

public List<List<Object>> combine(final List<List<Object>> containers) {
    return this.combineInternal(0, containers);
}

private List<List<Object>> combineInternal(final int currentIndex,
        final List<List<Object>> containers) {
    if (currentIndex == containers.size()) {
        // Skip the items for the last container
        final List<List<Object>> combinations = new ArrayList<>();
        combinations.add(Collections.emptyList());
        return combinations;
    }

    final List<List<Object>> combinations = new ArrayList<>();
    final List<Object> containerItemList = containers.get(currentIndex);
    // Get combination from next index
    final List<List<Object>> suffixList = this.combineInternal(
            currentIndex + 1, containers);

    final int size = containerItemList.size();
    for (int i = 0; i < size; i++) {
        final Object containerItem = containerItemList.get(i);
        if (suffixList != null) {
            for (final List<Object> suffix : suffixList) {
                final List<Object> nextCombination = new ArrayList<>();
                nextCombination.add(containerItem);
                nextCombination.addAll(suffix);
                combinations.add(nextCombination);
            }
        }
    }

    return combinations;
}

}

答案 3 :(得分:0)

只是简单 -

ArrayList<Integer> combination(int[] ar1, int[] ar2)
{
    ArrayList<Integer> result = new ArrayList<int>();
    for (int i1 : ar1)
    {
        for (int i2 : ar2)
        {
            result.add(i1 * i2);
        }
    }
    return result;
}

嗯,我认为递归是一般情况的解决方案。

import java.util.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        ArrayList<int[]> arrays = new ArrayList<int[]>();
        arrays.add(new int[] { 1, 2, 3 });
        arrays.add(new int[] { 4, 5 });
        arrays.add(new int[] { 6, 7 });

        ArrayList<Integer> result = combination(arrays);
        for (int i : result)
        {
            System.out.println(i);
        }
    }

    static ArrayList<Integer> combination(ArrayList<int[]> arrays)
    {
        if (arrays.size() == 0)
            return null;

        ArrayList<Integer> result = new ArrayList<Integer>();
        combination_sub(arrays, 0, result);
        return result;
    }
    static void combination_sub(ArrayList<int[]> arrays, int idx, ArrayList<Integer> result)
    {
        if (arrays.size() == idx)
        {
            result.add(1);
            return;
        }

        int[] ar = arrays.get(idx);

        ArrayList<Integer> sub_result = new ArrayList<Integer>();
        combination_sub(arrays, idx + 1, sub_result);

        for (int i : ar)
        {
            for (int sub_i : sub_result)
            {
                result.add(i * sub_i);
            }
        }
    }
}

答案 4 :(得分:0)

您可以在java 8 Stream Api中轻松实现此目的:

final

答案 5 :(得分:0)

如果是3个数组。如果要3个数组以外的其他数组,可以插入其他代码。递归解决方案

/* This code is contributed by Devesh Agrawal and modified for personal solution by prism */
/* contributed by Devesh Agrawal at https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/ */

package com.practice.probabilty;

import java.util.ArrayList;

public class CombinationNumber {

    public static void main(String[] args) {

        int[] arr1 = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] arr2 = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] arr3 = new int[] {0, 2, 4, 6, 8};

        int[] arr = arr1.clone();

        int r = 3; //amount of digits as need

        int n1 = arr1.length; //amount of all digits provided for composing the number
        int n2 = arr2.length;
        int n3 = arr3.length;


        int[] count = new int[]{0}; //for counting total amount of all combination numbers

        printCombination(arr1, arr2, arr3, arr, n1, n2, n3, r, count);

        System.out.println("Total amount of number = " + count[0]);

    }

    /*
     * arr[] ---> Input Array data[] ---> Temporary array to store current
     * combination start & end ---> Staring and Ending indexes in arr[] index --->
     * Current index in data[] r ---> Size of a combination to be printed
     */
    static void combinationUtil_1(int arr1[], int arr2[], int arr3[], int arr[],
            int n1, int n2, int n3, int r, int index,
            int data[], int i1, int i2, int i3, int[] count) {
        // Print index and i
        // System.out.print("data index = " + index + ", arr i = " + i + ", order  = " + order);
        // Current combination is ready to be printed, print it 

        if (index == r) {
            for (int j = 0; j < r; j++) {
                System.out.print(" " + data[j] + " ");

            }

            System.out.println();
            count[0]++;

            return;
        }

        // When no more elements are there to put in data[] 
        if (i1 >= n1 || i2 >= n2 || i3 >= n3)
            return;

        // current is included, put next at next location 

        if(index == 0)
        {

            data[index] = arr1[i1];

            combinationUtil_1(arr1, arr2, arr3, arr2, n1, n2, n3, r, index + 1, data, i1++, i2, i3, count);

            combinationUtil_1(arr1, arr2, arr3, arr2, n1, n2, n3, r, index, data, i1++, i2, i3, count);



        }else if(index == 1)
        {

            data[index] = arr2[i2];

            combinationUtil_1(arr1, arr2, arr3, arr3, n1, n2, n3, r, index + 1, data, i1, i2++, i3, count);

            combinationUtil_1(arr1, arr2, arr3, arr3, n1, n2, n3, r, index, data, i1, i2++, i3, count);

        }
        else
        {

            data[index] = arr3[i3];

            combinationUtil_1(arr1, arr2, arr3, arr1, n1, n2, n3, r, index + 1, data, i1, i2, i3++, count);

            combinationUtil_1(arr1, arr2, arr3, arr1, n1, n2, n3, r, index, data, i1, i2, i3++, count);

        }
    }

    // The main function that prints all combinations of size r
    // This function mainly uses combinationUtil()
    static void printCombination(int arr1[], int arr2[], int arr3[], int arr[], 
            int n1, int n2, int n3, int r, int[] count) {
        // A temporary array to store all combination one by one
        int data[] = new int[r];

        // Print all combination using temporary array 'data[]'
        combinationUtil_1(arr1, arr2, arr3, arr, n1, n2, n3, r, 0, data, 0, 0, 0, count);
    }

}

答案 6 :(得分:0)

对于几个数组,在此代码中为4个数组,递归

package com.practice.probabilty;

import java.util.ArrayList;
import java.util.HashMap;

public class Example {

    public static void main(String[] args) {

        int[] arr1 = new int[] {1, 2, 3, 4};
        int[] arr2 = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] arr3 = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] arr4 = new int[] {1, 3, 5, 7, 9};

        ArrayList<int[]> listOfArray = new ArrayList<int[]>();
        listOfArray.add(arr1);
        listOfArray.add(arr2);
        listOfArray.add(arr3);
        listOfArray.add(arr4);


        int sizeOfList = listOfArray.size();
        int r = sizeOfList;

        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i = 0; i < sizeOfList; i++)
        {
            map.put(i, 0);
        }

        int[] count = new int[]{0}; //for counting total amount of all combination numbers

        printCombination(listOfArray, sizeOfList, r, map, count);

        System.out.println("Total amount of number = " + count[0]);

    }

    /*
     * arr[] ---> Input Array data[] ---> Temporary array to store current
     * combination start & end ---> Staring and Ending indexes in arr[] index --->
     * Current index in data[] r ---> Size of a combination to be printed
     */

    static void combinationUtil_1(ArrayList<int[]> listOfArray,
            int sizeOfList, int r, int index,
            int data[], HashMap<Integer, Integer> map, int[] count) {

        if (index == r) {
//System.out.print("--> ");
            for (int j = 0; j < r; j++) {
                System.out.print(" " + data[j] + " ");

            }

            System.out.println();
            count[0]++;

            return;
        }

        for(int i = 0; i < sizeOfList; i++)
        {
            if(map.get(i) >= listOfArray.get(i).length)
                return;
        }

            data[index] = listOfArray.get(index)[map.get(index)];

            combinationUtil_1(listOfArray, sizeOfList, r, index + 1, data, map, count);
            HashMap<Integer, Integer> map1 = new HashMap<Integer, Integer>();
            for(int i = 0; i < sizeOfList; i++)
            {
                if(i == index)
                {
                    map1.put(i, map.get(i) + 1);
                }
                else
                {
                    map1.put(i, map.get(i));
                }
            }


            combinationUtil_1(listOfArray, sizeOfList, r, index, data, map1, count);
            HashMap<Integer, Integer> map2 = new HashMap<Integer, Integer>();
            for(int i = 0; i < sizeOfList; i++)
            {
                if(i == index)
                {
                    map2.put(i, map.get(i) + 1);
                }
                else
                {
                    map2.put(i, map.get(i));
                }
            }

    }

    static void printCombination(ArrayList<int[]> listOfArray, 
            int sizeOfList, int r, HashMap<Integer, Integer> map, int[] count) {
        // A temporary array to store all combination one by one
        int data[] = new int[r];

        // Print all combination using temporary array 'data[]'

        combinationUtil_1(listOfArray, sizeOfList, r, 0, data, map, count);
        //combinationUtil_1(arr1, arr2, arr3, arr, n1, n2, n3, r, 0, data, 0, 0, 0, count);
    }

    /* Driver function to check for above function */

}
/* This code is modified from Devesh Agrawal's */