zoukankan      html  css  js  c++  java
  • 将合并的数据结构来实现一个单一的列表

    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_coding

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    margin塌陷(collapse)
    this的值
    变量、函数声明提升
    Git与Svn的区别—笔记1
    ECMAScript 总结
    正则表达式
    i2c 通信
    player/stage 学习---安装
    各种分区类型对应的partition_Id
    ubuntu 映射网络驱动器到本地
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4806355.html
Copyright © 2011-2022 走看看