2010-06-16 29 views
2
public class ListMerge 
{ 
    public static void main(String[] args) 
    { 

     Scanner input = new Scanner(System.in); 

     System.out.println ("Input length of arraylist 1:"); 
     int n = input.nextInt(); 
     ArrayList x = new ArrayList(); 
     ArrayList y = new ArrayList(); 
     for (int i = 0; i < n; i++) 
     { 
     System.out.println ("Input x[ " + i +"] :"); 
     x.add(new Integer(i)); 

     } 


     System.out.println ("Input length of arraylist 2:"); 
     int m = input.nextInt(); 


     for (int i = 0; i < m; i++) 
     { 
     System.out.println ("Input y[ " + i +"] :"); 
     y.add(new Integer(i)); 

     } 
     List<Integer> all = new ArrayList<Integer>(); 

     all.addAll(x); 
     all.addAll(y); 
     System.out.println(all); 


    } 
} 

我做了這個 但它沒有從用戶獲取值。 請告訴我爲什麼....如何合併兩個有序的對象列表?

+0

按您的意思排序?你需要將兩個排序列表合併到一個排序列表中? – polygenelubricants 2010-06-16 11:10:56

+0

yes.i需要合併兩個已排序的列表 – 2010-06-16 11:16:38

+0

您的意思是「它不從用戶獲取值」是什麼意思? – Kayz 2014-02-18 15:52:43

回答

4

直接從wikipedia

function merge(left,right) 
    var list result 
    while length(left) > 0 and length(right) > 0 
     if first(left) ≤ first(right) 
      append first(left) to result 
      left = rest(left) 
     else 
      append first(right) to result 
      right = rest(right) 
    end while 
    if length(left) > 0 
     append left to result 
    else 
     append right to result 
    return result 
+0

這是一個mergesort。他只想合併列表,即「向左追加結果,向右追加結果,返回結果」。 – 2010-06-16 08:16:30

+1

這是mergesort的一部分,當然。我想,他需要合併兩個有序列表,使其保持有序。這正是mergesort的這一步所做的 – unbeli 2010-06-16 08:18:47

+0

他雖然在他原來的問題中沒有提到任何順序。我只是指出,如果他無法弄清楚如何加入兩個列表,他不會理解mergesort。 – 2010-06-17 02:24:00

0

要完成知識的圓圈,下面是@ unbeli的/維基百科的答案的實現。

public List<T> mergeOrdered(final List<T> list0, final List<T> list1) { 
    List<T> result = new ArrayList<T>(); 

    while (list0.size() > 0 && list1.size() > 0) { 
    if (list0.get(0).compareTo(list1.get(0)) < 0) { 
      result.add(list0.get(0)); 
      list0.remove(0); 
     } 
     else { 
      result.add(list1.get(0)); 
      list1.remove(0); 
     } 
    } 

    if (list0.size() > 0) { 
     result.addAll(list0); 
    } 
    else if (list1.size() > 0) { 
     result.addAll(list1); 
    } 

    return result; 
} 

注意列出使用Arrays.asList()將導致fixed length陣列由哪些元素不能被刪除創建的 - 所以一定要確保你傳遞動態列表!

public List<T> buildDynamicListFromArray(final T[] arr0) { 
    List<T> result = new ArrayList<T>(); 
    int len = arr0.length; 
    for (int i = 0; i < len; i++) { 
     result.add(arr0[i]); 
    } 
    return result; 
} 
0

這裏使用可能更有效的迭代器,結果ArrayList中,自定義比較器的預ALLOC,和輸入列表上無副作用的實現:

public static <T, C extends Comparator<T>> List<T> mergeOrderedAscending(List<T> list0, List<T> list1, C c) 
{ 
    if(list0 == null || list1 == null || c == null) 
     throw new IllegalArgumentException("null parameter"); 

    Iterator<T> it0 = list0.iterator(); 
    Iterator<T> it1 = list1.iterator(); 

    List<T> result = new ArrayList<T>(list0.size() + list1.size()); 


    T i0 = null; 
    T i1 = null; 


    if(it0.hasNext()) 
     i0 = it0.next(); 
    else 
     i0 = null; 


    if(it1.hasNext()) 
     i1 = it1.next(); 
    else 
     i1 = null; 

    while (i0 != null && i1 != null) { 

     if (c.compare(i0, i1) < 0) { 
      result.add(i0); 

      if(it0.hasNext()) 
       i0 = it0.next(); 
      else 
       i0 = null; 

     } 
     else { 
      result.add(i1); 

      if(it1.hasNext()) 
       i1 = it1.next(); 
      else 
       i1 = null; 
     } 
    } 


    if (i0 != null) { 

     do { 
      result.add(i0); 

      if(it0.hasNext()) 
       i0 = it0.next(); 
      else 
       i0 = null; 
     } while(i0 != null); 
    } 
    else if (i1 != null) { 
     do { 
      result.add(i1); 

      if(it1.hasNext()) 
       i1 = it1.next(); 
      else 
       i1 = null; 
     } while(i1 != null); 
    } 

    return result; 
} 
1

隨着Google Guava

Iterable<Integer> all = 
    Iterables.mergeSorted(ImmutableList.of(x, y), Ordering.natural()); 
System.out.println(all);