码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-06-16 23:12:27      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   java   http   com   

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

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

标签:class   blog   code   java   http   com   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!