zoukankan      html  css  js  c++  java
  • Java集合框架—List

    Collection

    |--List:元素是有序的,元素可以重复。因为该集合体系有索引。
    |--ArrayList:底层的数据结构使用的是数组结构。特点:查询速度很快。但是增删稍慢。线程不同步。
    |--LinkedList:底层使用的链表数据结构。特点:增删速度很快,查询稍慢。线程不同步。
    |--Vector:底层是数组数据结构。线程同步。被ArrayList替代了。因为效率低。

    |--Set:元素是无序,元素不可以重复。、


    Collection定义了集合框架的共性功能。
    1,添加
    add(e);
    addAll(collection);


    2,删除
    remove(e);
    removeAll(collection);
    clear();


    3,判断。
    contains(e);
    isEmpty();


    4,获取
    iterator();
    size();


    5,获取交集。
    retainAll();


    6,集合变数组。
    toArray();



    List:
    特有方法。凡是可以操作角标的方法都是该体系特有的方法。



    add(index,element);
    addAll(index,Collection);



    remove(index);



    set(index,element);

    get(index):
    subList(from,to);
    listIterator();
    int indexOf(obj):获取指定元素的位置。
    ListIterator listIterator();


    LinkedList:特有方法:
    addFirst();
    addLast();


    getFirst();
    getLast();
    获取元素,但不删除元素。如果集合中没有元素,会出现NoSuchElementException

    removeFirst();
    removeLast();
    获取元素,但是元素被删除。如果集合中没有元素,会出现NoSuchElementException


    在JDK1.6出现了替代方法。

    offerFirst();
    offerLast();

    peekFirst();
    peekLast();
    获取元素,但不删除元素。如果集合中没有元素,会返回null。



    pollFirst();
    pollLast();
    获取元素,但是元素被删除。如果集合中没有元素,会返回null。




    List集合特有的迭代器是ListIterator,其是Iterator的子接口。

    在迭代时,不可以通过集合对象的方法操作集合中的元素。因为会发生ConcurrentModificationException异常。

    所以,在迭代器时,只能用迭代器的放过操作元素,可是Iterator方法是有限的,只能对元素进行判断,取出,删除的操作,如果想要其他的操作如添加,修改等,就需要使用其子接口,ListIterator。该接口只能通过List集合的listIterator方法获取。



    问题1、如何去除ArrayList中的重复元素?

    思路:新建一个集合ArrayList2,将ArrayList1中的元素依次进行判断,若其不再ArrayList2中,则将其add到ArrayList2,最后ArrayList2即为去除了重复元素的集合。

    代码如下:

    import java.util.*;
    
    /*
    去除ArrayList集合中的重复元素。
    
    */
    
    class ArrayListTest 
    {
    
    	public static void sop(Object obj)
    	{
    		System.out.println(obj);
    	}
    	public static void main(String[] args) 
    	{
    		ArrayList al = new ArrayList();
    
    		al.add("java01");
    		al.add("java02");
    		al.add("java01");
    		al.add("java02");
    		al.add("java01");
    //		al.add("java03");
    
    
    		/*
    		在迭代时循环中next调用一次,就要hasNext判断一次。
    		Iterator it = al.iterator();
    
    		while(it.hasNext())
    		{
    			sop(it.next()+"...."+it.next());
    		}
    		*/
    
    		/**/
    		sop(al);
    		
    		al = singleElement(al);
    
    		sop(al);
    		
    
    	}
    
    	public static ArrayList singleElement(ArrayList al)
    	{
    		//定义一个临时容器。
    		ArrayList newAl = new ArrayList();
    
    		Iterator it = al.iterator();
    
    		while(it.hasNext())
    		{
    			Object obj = it.next();
    
    			if(!newAl.contains(obj))
    				newAl.add(obj);
    
    		}
    
    		return newAl;
    	}
    }






  • 相关阅读:
    js中==与===区别
    Initialization failure 0x0000000c
    Spring通过@Value注解注入属性的几种方式
    java中读取配置文件中数据的具体方法
    spring整合hibernate
    url上参数解析笔记
    编号的生成(日期+序列号)
    application.xml & -servlet.xml
    webApplicationContext 与servletContext
    Http协议整理
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6467327.html
Copyright © 2011-2022 走看看