javaSE-常用类-集合类List系列

Collection:
|--List
|--Set



List:
1,是有序的,存入的顺序和取出的顺序一致。
2,元素是有索引的。
3,元素可以重复。

了解List接口的特有方法,注意:这些特有方法都是围绕着角标定义的。
1,add(index,element);

2,remove(index):

3,set(index,element);

4,get(index);

List接口支持,增删改查操作。

List
|--Vector:数组结构的,是同步的。被ArrayList替代,因为效率低。

|--ArrayList:数据结构是数组结构,是不同步的。查询速度很快。

|--LinkedList:数据结构是链表结构,是不同步的。增删速度很快。homework


/*
	 * 演示List接口。
	 */
	public static void listDemo(List list){
		
		//1,添加元素。
		list.add("abc3");
		list.add("abc9");
		list.add("abc1");
		list.add("abc4");
		
		//2,指定位置增加元素。
//		list.add(2,"abc2");
		
		//3,删除指定角标元素。
//		list.remove(4);
		
		//4,修改指定位置的元素。
//		list.set(2,"abc77");
		
		//5,获取指定位置的元素。
		System.out.println(list.get(1));
		
		System.out.println(list);
		
		
	}
public static void main(String[] args) {

		/*
		 * 取出list中的所有元素。
		 * 
		 */
		List list = new ArrayList();
		
		list.add("abc1");
		list.add("abc3");
		list.add("abc2");
		list.add("abc6");
		
//		for(Iterator it = list.iterator(); it.hasNext(); ){
//			System.out.println(it.next());
//		}
		
		//List特有的取出所有元素的方式。
		for (int i = 0; i < list.size(); i++) {
			System.out.println("get("+i+"):"+list.get(i));
		}
	}

public static void main(String[] args) {

		List list = new ArrayList();

		list.add("abc1");
		list.add("abc2");
		list.add("abc3");
		list.add("abc4");
		
		
//		Iterator it = list.iterator();
		
		ListIterator it = list.listIterator();
		/*
		 * 列表迭代器是List集合的特有迭代器,可以在迭代过程中实现增删改查。
		 */
		
		while(it.hasNext()){
			Object obj = it.next();//ConcurrentModificationException
			if(obj.equals("abc2")){
//				list.add("abc9");
				it.add("abc9");
			}
		}
		
		System.out.println(list);
	}

java.util
类 ConcurrentModificationException

当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。

例如,某个线程在 Collection 上进行迭代时,通常不允许另一个线性修改该 Collection。通常在这些情况下,迭代的结果是不确定的。如果检测到这种行为,一些迭代器实现(包括 JRE 提供的所有通用 collection 实现)可能选择抛出此异常。执行该操作的迭代器称为快速失败 迭代器,因为迭代器很快就完全失败,而不会冒着在将来某个时间任意发生不确定行为的风险。

注意,此异常不会始终指出对象已经由不同 线程并发修改。如果单线程发出违反对象协定的方法调用序列,则该对象可能抛出此异常。例如,如果线程使用快速失败迭代器在 collection 上迭代时直接修改该 collection,则迭代器将抛出此异常。

注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证。快速失败操作会尽最大努力抛出 ConcurrentModificationException。因此,为提高此类操作的正确性而编写一个依赖于此异常的程序是错误的做法,正确做法是:ConcurrentModificationException 应该仅用于检测 bug。


public static void main(String[] args) {

		
		//创建一个ArrayList集合对象。
		ArrayList al = new ArrayList();
		
		//添加Person对象,到集合中,其实集合中记录是对象的引用。
		al.add(new Person("lisi1",21));//add(Object obj)
		al.add(new Person("lisi6",26));
		al.add(new Person("lisi2",22));
		al.add(new Person("lisi7",27));
		
		//取出所有Person对象。
		for (Iterator it = al.iterator(); it.hasNext();) {
			Person p = (Person)it.next();//取元素时,要强转才可以使用子类特有方法。
			System.out.println(p.getName()+","+p.getAge());
		}

public static void main(String[] args) {

		/*
		 * LinkedList特有。
		 * 
		 * addFirst();
		 * addLast();
		 * 
		 * getFirst();
		 * getLast();
		 * 
		 * removeFirst();
		 * removeLast();
		 */
		
		//创建一个链表对象。
		LinkedList link = new LinkedList();
		link.addFirst("abc1");
		link.addFirst("abc2");
		link.addFirst("abc3");
		link.addFirst("abc4");
		
//		System.out.println(link.removeFirst());//abc4
//		System.out.println(link.removeFirst());//abc3
//		System.out.println(link.getFirst());//abc4
//		System.out.println(link.getFirst());//abc4
//		System.out.println(link);
		
		while(!link.isEmpty()){
			System.out.println("link:"+link.removeLast());
		}
		
	}

public static void main(String[] args) {

		/*
		 * LinkedList特有。
		 * 
		 * addFirst(); addLast();
		 * 
		 * getFirst(); getLast();
		 * 
		 * removeFirst(); removeLast();
		 */

		// 创建一个链表对象。
		LinkedList link = new LinkedList();
		link.addFirst("abc1");
		link.addFirst("abc2");
		link.addFirst("abc3");
		link.addFirst("abc4");

		// System.out.println(link.removeFirst());//abc4
		// System.out.println(link.removeFirst());//abc3
		// System.out.println(link.getFirst());//abc4
		// System.out.println(link.getFirst());//abc4
		// System.out.println(link);

		while (!link.isEmpty()) {
			System.out.println("link:" + link.removeLast());
		}

	}










java.util

Interface List<E>

Method Summary

Methods  
Modifier and Type Method and Description
boolean add(E e)
Appends the specified element to the end of this list (optional operation).
void add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation).
boolean addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
boolean addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
void clear()
Removes all of the elements from this list (optional operation).
boolean contains(Object o)
Returns  true if this list contains the specified element.
boolean containsAll(Collection<?> c)
Returns  true if this list contains all of the elements of the specified collection.
boolean equals(Object o)
Compares the specified object with this list for equality.
E get(int index)
Returns the element at the specified position in this list.
int hashCode()
Returns the hash code value for this list.
int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
boolean isEmpty()
Returns  true if this list contains no elements.
Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.
int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
ListIterator<E> listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E> listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
E remove(int index)
Removes the element at the specified position in this list (optional operation).
boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present (optional operation).
boolean removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection (optional operation).
boolean retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection (optional operation).
E set(int index, E element)
Replaces the element at the specified position in this list with the specified element (optional operation).
int size()
Returns the number of elements in this list.
List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified  fromIndex, inclusive, and  toIndex, exclusive.
Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

java.util
类 ArrayList<E>

  • Constructor Summary

    Constructors  
    Constructor and Description
    ArrayList()
    Constructs an empty list with an initial capacity of ten.
    ArrayList(Collection<? extends E> c)
    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
    ArrayList(int initialCapacity)
    Constructs an empty list with the specified initial capacity.
  • Method Summary

    Methods  
    Modifier and Type Method and Description
    boolean add(E e)
    Appends the specified element to the end of this list.
    void add(int index, E element)
    Inserts the specified element at the specified position in this list.
    boolean addAll(Collection<? extends E> c)
    Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
    boolean addAll(int index, Collection<? extends E> c)
    Inserts all of the elements in the specified collection into this list, starting at the specified position.
    void clear()
    Removes all of the elements from this list.
    Object clone()
    Returns a shallow copy of this  ArrayList instance.
    boolean contains(Object o)
    Returns  true if this list contains the specified element.
    void ensureCapacity(int minCapacity)
    Increases the capacity of this  ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
    E get(int index)
    Returns the element at the specified position in this list.
    int indexOf(Object o)
    Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
    boolean isEmpty()
    Returns  true if this list contains no elements.
    Iterator<E> iterator()
    Returns an iterator over the elements in this list in proper sequence.
    int lastIndexOf(Object o)
    Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
    ListIterator<E> listIterator()
    Returns a list iterator over the elements in this list (in proper sequence).
    ListIterator<E> listIterator(int index)
    Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
    E remove(int index)
    Removes the element at the specified position in this list.
    boolean remove(Object o)
    Removes the first occurrence of the specified element from this list, if it is present.
    boolean removeAll(Collection<?> c)
    Removes from this list all of its elements that are contained in the specified collection.
    protected void removeRange(int fromIndex, int toIndex)
    Removes from this list all of the elements whose index is between  fromIndex, inclusive, and  toIndex, exclusive.
    boolean retainAll(Collection<?> c)
    Retains only the elements in this list that are contained in the specified collection.
    E set(int index, E element)
    Replaces the element at the specified position in this list with the specified element.
    int size()
    Returns the number of elements in this list.
    List<E> subList(int fromIndex, int toIndex)
    Returns a view of the portion of this list between the specified  fromIndex, inclusive, and  toIndex, exclusive.
    Object[] toArray()
    Returns an array containing all of the elements in this list in proper sequence (from first to last element).
    <T> T[] toArray(T[] a)
    Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
    void trimToSize()
    Trims the capacity of this  ArrayList instance to be the list's current size.
java.util

Class LinkedList<E>


  • Constructor Summary

    Constructors  
    Constructor and Description
    LinkedList()
    Constructs an empty list.
    LinkedList(Collection<? extends E> c)
    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
  • Method Summary

    Methods  
    Modifier and Type Method and Description
    boolean add(E e)
    Appends the specified element to the end of this list.
    void add(int index, E element)
    Inserts the specified element at the specified position in this list.
    boolean addAll(Collection<? extends E> c)
    Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
    boolean addAll(int index, Collection<? extends E> c)
    Inserts all of the elements in the specified collection into this list, starting at the specified position.
    void addFirst(E e)
    Inserts the specified element at the beginning of this list.
    void addLast(E e)
    Appends the specified element to the end of this list.
    void clear()
    Removes all of the elements from this list.
    Object clone()
    Returns a shallow copy of this  LinkedList.
    boolean contains(Object o)
    Returns  true if this list contains the specified element.
    Iterator<E> descendingIterator()
    Returns an iterator over the elements in this deque in reverse sequential order.
    E element()
    Retrieves, but does not remove, the head (first element) of this list.
    E get(int index)
    Returns the element at the specified position in this list.
    E getFirst()
    Returns the first element in this list.
    E getLast()
    Returns the last element in this list.
    int indexOf(Object o)
    Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
    int lastIndexOf(Object o)
    Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
    ListIterator<E> listIterator(int index)
    Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
    boolean offer(E e)
    Adds the specified element as the tail (last element) of this list.
    boolean offerFirst(E e)
    Inserts the specified element at the front of this list.
    boolean offerLast(E e)
    Inserts the specified element at the end of this list.
    E peek()
    Retrieves, but does not remove, the head (first element) of this list.
    E peekFirst()
    Retrieves, but does not remove, the first element of this list, or returns  null if this list is empty.
    E peekLast()
    Retrieves, but does not remove, the last element of this list, or returns  null if this list is empty.
    E poll()
    Retrieves and removes the head (first element) of this list.
    E pollFirst()
    Retrieves and removes the first element of this list, or returns  null if this list is empty.
    E pollLast()
    Retrieves and removes the last element of this list, or returns  null if this list is empty.
    E pop()
    Pops an element from the stack represented by this list.
    void push(E e)
    Pushes an element onto the stack represented by this list.
    E remove()
    Retrieves and removes the head (first element) of this list.
    E remove(int index)
    Removes the element at the specified position in this list.
    boolean remove(Object o)
    Removes the first occurrence of the specified element from this list, if it is present.
    E removeFirst()
    Removes and returns the first element from this list.
    boolean removeFirstOccurrence(Object o)
    Removes the first occurrence of the specified element in this list (when traversing the list from head to tail).
    E removeLast()
    Removes and returns the last element from this list.
    boolean removeLastOccurrence(Object o)
    Removes the last occurrence of the specified element in this list (when traversing the list from head to tail).
    E set(int index, E element)
    Replaces the element at the specified position in this list with the specified element.
    int size()
    Returns the number of elements in this list.
    Object[] toArray()
    Returns an array containing all of the elements in this list in proper sequence (from first to last element).
    <T> T[] toArray(T[] a)
    Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值