Combining two Arraylists into one Array
Suppose you have two arrayLists: A and B.
How do I create a new array which is the same size as B and stores the index values of A as integers in sequential order.
So say for example the size of B is 5 and A has 3 values.
A[0] = Ra
A[1] = Be
A[2] = Ce
B[0] = F
B[1] = M
B[2] = K
B[3] = P
B[4] = L
I want to then create in java different possible versions (say 5 versions) of an arrayC of size 5 (the same size as listB) with a sequential ordering of the indexes of listA.
So like:
arrayC[0] = 0
arrayC[1] = 1
arrayC[2] = 1
arrayC[3] = 2
arrayC[4] = 2
or
arrayC[0] = 0
arrayC[1] = 0
arrayC[2] = 1
arrayC[3] = 2
arrayC[4] = 2
are both valid combinations in arrayC. However
arrayC[0] = 0
开发者_StackOverflowarrayC[1] = 2
arrayC[2] = 1
arrayC[3] = 2
arrayC[4] = 2
is not.
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);
}
Alright, assuming I'm understanding the question correctly, here's a program that will print every combination of 5 numbers ranging from 0 to 2. The getAllCombinations
method is general-purpose, so you can simply alter the values to see different results.
A word of warning: this uses recursion and calculates all results, so it's not very efficient. This is just to get you on your way.
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;
}
}
This method returns all orderings. Limit it if you just want a few.
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);
}
Test code:
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);
}
Output:
[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 demo:
Link
精彩评论