package com.he.list; public class Collections { public static ArrayList mergeList(ArrayList l1, ArrayList l2) { ArrayList l = new ArrayList(); int l1_length = l1.getLength(); int l2_length = l2.getLength(); int i = 0; int j = 0; while (i < l1_length && j < l2_length) { if (l1.get(i) < l2.get(j)) { l.add(l1.get(i)); i++; } else { l.add(l2.get(j)); j++; } } while (i < l1_length) { l.add(l1.get(i)); i++; } while (j < l2_length) { l.add(l2.get(j)); j++; } return l; } public static void main(String[] args) { ArrayList l1 = new ArrayList(); ArrayList l2 = new ArrayList(); for (int i = 0; i < 20; i++) { if (i % 2 == 0) { l1.add(i); } else l2.add(i); } System.out.println("这是列表1:"); for (int i = 0; i < l1.getLength(); i++) { System.out.print(l1.get(i) + " "); } System.out.println(); System.out.println("这是列表2:"); for (int i = 0; i < l2.getLength(); i++) { System.out.print(l2.get(i) + " "); } l1 = Collections.mergeList(l1, l2); System.out.println(); System.out.println("归并俩个列表:"); for (int i = 0; i < l1.getLength(); i++) { System.out.print(l1.get(i) + " "); } } }ArrayList实现请参照前面的博文,更多内容请关注小猿的微信公众号:love_coding
原文:http://blog.csdn.net/superstonne/article/details/31506101