首页 > 其他 > 详细

数据结构单列表的合并实现

时间:2014-06-16 23:12:00      阅读:411      评论:0      收藏:0      [点我收藏+]
package com.he.list;

public class Collections {

	public static ArrayList union(ArrayList l1, ArrayList l2) {
		int l2_length = l2.getLength();
		for (int i = 0; i < l2_length; i++) {
			if (!l1.contains(l2.get(i))) {
				l1.add(l2.get(i));
			}

		}
		return l1;
	}

	public static void main(String[] args) {
		ArrayList l1 = new ArrayList();
		ArrayList l2 = new ArrayList();
		for (int i = 0; i < 20; i++) {
			if (i < 10) {
				l1.add(i);
			}
			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.union(l1, l2);
		System.out.println();
		System.out.println("合并俩个列表:");
		for (int i = 0; i < l1.getLength(); i++) {
			System.out.print(l1.get(i) + " ");
		}
	}
}
ArrayList的实现请参考上篇博文,更多内容请关注小猿公众号:love_codingbubuko.com,布布扣

数据结构单列表的合并实现,布布扣,bubuko.com

数据结构单列表的合并实现

原文:http://blog.csdn.net/superstonne/article/details/31049709

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!