2013-01-16 32 views
-1

說三個列表元素的新名單上有三個列表:建立從特定元素的順序

a = {a, b, c, d, e} 
b = {A, B, C} 
c = {aa, bb, cc, dd} 

現在我需要從每個列表需要三個元素並將其插入到一個新的列表,從列表中到c。所以新的列表應該是:

newlist = {a, b, c, A, B, C, aa, bb, cc, d, e, dd} 

我想知道在Java中這樣做的最快方法是什麼?

[更新]
統一數據類型以使需求更清晰。

+0

第一個列表中的元素是否必須是列表本身? (a = a,b = b,c = c)? – Gangnus

回答

0

使用Iterables.concat(Iterable<T> ...),它創建所有迭代的實時視圖,連接成一個(如果更改迭代,連接的版本也會更改)。然後用Iterables.unmodifiableIterable(Iterable<T>)包裝連接的迭代器(我以前沒有看到只讀要求)。

Iterables.concat(..)的JavaDoc:

將多個iterables成一個單一的可迭代。返回的迭代器有一個遍歷輸入中每個迭代元素的迭代器。直到必要時纔會輪詢輸入迭代器。當相應的輸入迭代器支持時,返回的迭代器的迭代器支持remove()。

雖然這並沒有明確說這是一個實時取景,最後一句意味着它是(僅支持如果支撐迭代器支持的話是不可能的,除非使用實時取景的Iterator.remove()法)

示例代碼:

final List<Integer> first = Lists.newArrayList(1, 2, 3); 
final List<Integer> second = Lists.newArrayList(4, 5, 6); 
final List<Integer> third = Lists.newArrayList(7, 8, 9); 
final Iterable<Integer> all = 
    Iterables.unmodifiableIterable(
     Iterables.concat(first, second, third)); 
System.out.println(all); 
third.add(9999999); 
System.out.println(all); 

輸出:

[1, 2, 3, 4, 5, 6, 7, 8, 9] 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 999, 9999] 

得到結果後,您可以進行排序的單一列表。

合併排序真的很容易。

/** 
* Mergesort algorithm. 
* @param a an array of Comparable items. 
*/ 
public static void mergeSort(Comparable [ ] a) { 
    Comparable [ ] tmpArray = new Comparable[ a.length ]; 
    mergeSort(a, tmpArray, 0, a.length - 1); 
} 

/** 
* Internal method that makes recursive calls. 
* @param a an array of Comparable items. 
* @param tmpArray an array to place the merged result. 
* @param left the left-most index of the subarray. 
* @param right the right-most index of the subarray. 
*/ 
private static void mergeSort(Comparable [ ] a, Comparable [ ] tmpArray, 
     int left, int right) { 
    if(left < right) { 
     int center = (left + right)/2; 
     mergeSort(a, tmpArray, left, center); 
     mergeSort(a, tmpArray, center + 1, right); 
     merge(a, tmpArray, left, center + 1, right); 
    } 
} 

/** 
* Internal method that merges two sorted halves of a subarray. 
* @param a an array of Comparable items. 
* @param tmpArray an array to place the merged result. 
* @param leftPos the left-most index of the subarray. 
* @param rightPos the index of the start of the second half. 
* @param rightEnd the right-most index of the subarray. 
*/ 
private static void merge(Comparable [ ] a, Comparable [ ] tmpArray, 
     int leftPos, int rightPos, int rightEnd) { 
    int leftEnd = rightPos - 1; 
    int tmpPos = leftPos; 
    int numElements = rightEnd - leftPos + 1; 

    // Main loop 
    while(leftPos <= leftEnd && rightPos <= rightEnd) 
     if(a[ leftPos ].compareTo(a[ rightPos ]) <= 0) 
      tmpArray[ tmpPos++ ] = a[ leftPos++ ]; 
     else 
      tmpArray[ tmpPos++ ] = a[ rightPos++ ]; 

    while(leftPos <= leftEnd) // Copy rest of first half 
     tmpArray[ tmpPos++ ] = a[ leftPos++ ]; 

    while(rightPos <= rightEnd) // Copy rest of right half 
     tmpArray[ tmpPos++ ] = a[ rightPos++ ]; 

    // Copy tmpArray back 
    for(int i = 0; i < numElements; i++, rightEnd--) 
     a[ rightEnd ] = tmpArray[ rightEnd ]; 
} 
+0

我認爲OP的意圖是不同的。列表'a'是整數,列表'b'和'c'是字符類型。 Atlest他們看起來像字符類型。 – Smit

+0

我的意圖是給出想法,而不是確切的答案。讓他去做更多的編碼.. – Sahal