将两个Arraylists组合成一个Array

时间:2011-06-22 10:43:09

标签: java

假设您有两个数组列表:A和B.

如何创建一个与B大小相同的新数组,并按顺序将A的索引值存储为整数。

所以说例如B的大小是5,A有3个值。

A[0] = Ra
A[1] = Be
A[2] = Ce


B[0] = F
B[1] = M
B[2] = K
B[3] = P
B[4] = L

我想在java中创建一个大小为5(与listB大小相同)的arrayC的不同可能版本(比如5个版本),并对listA的索引进行顺序排序。

所以喜欢:

arrayC[0] = 0
arrayC[1] = 1
arrayC[2] = 1
arrayC[3] = 2
arrayC[4] = 2

arrayC[0] = 0
arrayC[1] = 0
arrayC[2] = 1
arrayC[3] = 2
arrayC[4] = 2

是arrayC中的有效组合。然而

arrayC[0] = 0
arrayC[1] = 2
arrayC[2] = 1
arrayC[3] = 2
arrayC[4] = 2

不是。

3 个答案:

答案 0 :(得分:1)

    ArrayList a = new ArrayList();
    a.add(new Object());
    a.add(new Object());
    a.add(new Object());

    ArrayList b = new ArrayList();
    b.add(new Object());
    b.add(new Object());
    b.add(new Object());
    b.add(new Object());
    b.add(new Object());

    Random r = new Random();
    int c[] = new int[b.size()];
    int aIndex = 0;
    for(int i = 0; i <c.length; i++){

        if(i != 0) { //assume we always use aIndex = 0 for first element

            if((c.length - i) < a.size() - aIndex ){ //must increase the index
                aIndex++;
            }
            else if(r.nextBoolean() && aIndex < a.size()-1){ //Randomly increase the index
                aIndex++;
            }
        }
        c[i] = aIndex;
        System.out.print("\nC[" +i +"]:" + aIndex);
    }

答案 1 :(得分:1)

好吧,假设我正确理解了这个问题,这里有一个程序可以打印从0到2的5个数字的每个组合。getAllCombinations方法是通用的,所以你可以简单地改变这些值看到不同的结果。

警告:这使用递归并计算所有结果,因此效率不高。这只是为了让你继续前进。

package test;

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

public class Main {

    public static void main(final String[] args) {

        final int sizeA = 3;
        final int sizeB = 5;

        final List<int[]> combos = getAllCombinations(sizeA-1, sizeB);

        int counter = 1;
        for(final int[] combo : combos) {
            System.out.println("Combination " + counter);
            System.out.println("--------------");
            for(final int value : combo) {
                System.out.print(value + " ");
            }
            System.out.println();
            System.out.println();
            ++counter;
        }

    }

    private static List<int[]> getAllCombinations(final int maxIndex, final int size) {

        if(maxIndex >= size)
            throw new IllegalArgumentException("The maximum index must be smaller than the array size.");

        final List<int[]> result = new ArrayList<int[]>();

        if(maxIndex == 0) {
            final int[] array = new int[size];
            Arrays.fill(array, maxIndex);
            result.add(array);
            return result;
        }

        //We'll create one array for every time the maxIndex can occur while allowing
        //every other index to appear, then create every variation on that array
        //by having every possible head generated recursively
        for(int i = 1; i < size - maxIndex + 1; ++i) {

            //Generating every possible head for the array
            final List<int[]> heads = getAllCombinations(maxIndex - 1, size - i);

            //Combining every head with the tail
            for(final int[] head : heads) {
                final int[] array = new int[size];
                System.arraycopy(head, 0, array, 0, head.length);
                //Filling the tail of the array with i maxIndex values
                for(int j = 1; j <= i; ++j)
                    array[size - j] = maxIndex;
                result.add(array);
            }

        }

        return result;

    }

}

答案 2 :(得分:0)

此方法返回所有排序。如果您只是想要一些,请限制它。

public static Set<List<Integer>> orderings(int i, int len, int max) {

    Set<List<Integer>> seqs = new HashSet<List<Integer>>();

    if (len <= 0 || i > max)
        return seqs;

    if (max - i == len) {
        List<Integer> l = new ArrayList<Integer>();
        while (i < max)
            l.add(i++);
        seqs.add(l);
        return seqs;
    }

    seqs.addAll(orderings(i    , len - 1, max));
    seqs.addAll(orderings(i + 1, len - 1, max));

    for (List<Integer> l : seqs)
        l.add(0, i);

    return seqs;
}


public static Set<List<Integer>> orderings(int[] arr1, int[] arr2) {
    return orderings(0, arr2.length, arr1.length);
}

测试代码:

public static void main(String[] args) {
    int[] listA = { 0, 1, 2 };
    int[] listB = { 0, 1, 2, 3, 4 };
    for (List<Integer> seq : orderings(listA, listB))
        System.out.println(seq);
}

<强>输出:

[0, 0, 1, 2, 2]  <-- your second example
[0, 1, 1, 1, 2]
[0, 1, 1, 2, 2]  <-- your first example
[0, 1, 2, 2, 2]
[0, 0, 0, 1, 2]
[0, 0, 1, 1, 2]

Ideone.com演示:
Link