2011-06-22 65 views
2

假設你有兩個的ArrayList:A和B.組合兩個的ArrayList到一個數組

如何創建一個新的數組它的大小爲B相同,存儲的索引值按順序整數。

所以說例如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 

我想那麼在大小5的arrayC的(大小相同數組listB)用的listA的索引的順序排序java的不同可能版本(比如5個版本)創建。

所以像:

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 

不是。

+5

這個問題很混亂 - 你需要以更準確的方式來澄清(也許更多的例子?)你想達到什麼目的。我不能在這個問題上做出正面或反面的評論! – Chii

+0

對不起,我認爲這些例子很明顯。我會再試一次,這很難解釋它。 – Pandy

+0

所以如果'listA'更小,你想*隨機*重複一些值?是對的嗎?如果'listA'比'listB'更大*怎麼辦?並且'listB'的實際內容*有什麼意義?還是隻有它的大小? –

回答

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

+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); 
    } 
+0

...編輯添加數組列表設置。現在的問題指定的ArrayList到一個數組 – Steve

+0

謝謝,我已經嘗試通過添加字符串,但沒有打印...編寫本 – Pandy

+0

你不需要添加任何東西,只是包裝在一個main()上面的代碼或單元測試和運行它。它會每次生成一個新的隨機有效輸出。歡呼史蒂夫 – Steve

1

好吧,假設我在正確認識這個問題,這裏有一個程序,將打印5個數字,從0到2的每個組合的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; 

    } 

} 
+0

你如何修改它,以便隨機計算結果? – Pandy

+0

@Pandy有一兩件事你可以做的是讓'List'與'getAllCombinations',然後隨機選擇一堆從中陣列。儘管如此,仍然計算每種可能的組合。 –