目录
一、方法展示
二、add() 方法介绍
2.1.add(E element)
2.1.1源码
2.1.2.实例截图
2.1.3.Null引起的题外话
2.2.add(int index, E element)
2.2.1.源码
2.2.2.示例截图
2.2.3. add()引起IndexOutOfBoundsException简介
三、addAll()方法介绍
3.1.addAll
3.1.1.源码
3.1.2.代码示例
3.1.3.运行截图
四、set(int index,E element)
4.1.元素替换方法:set(int index,E element)
4.1.1.源码
一、方法展示
由上图可以看出,Java中线性表List接口的插入方法大概分为三大类,add、addAll和set。
二、add() 方法介绍
2.1.add(E element)
2.1.1源码
/*** Appends the specified element to the end of this list (optional* operation).* 将指定的元素追加到此list的末尾(可选操作)** <p>Lists that support this operation may place limitations on what* elements may be added to this list. * <p>支持此操作的列表可能会限制元素可以被添加到该list中。** In particular, some lists will refuse to add null elements, and others will * impose restrictions on the type of elements that may be added. * 特别是一些list将拒绝添加null元素,其他列表将强制对可以添加的元素类型的限制。* * List classes should clearly specify in their documentation any restrictions* on what elements may be added.* List类应该在其文档中明确规定任何限制关于可以添加哪些元素。** @param e element to be appended to this list* @return <tt>true</tt> (as specified by {@link Collection#add})* @throws UnsupportedOperationException if the <tt>add</tt> operation* is not supported by this list* @throws ClassCastException if the class of the specified element* prevents it from being added to this list* @throws NullPointerException if the specified element is null and this* list does not permit null elements* @throws IllegalArgumentException if some property of this element* prevents it from being added to this list*/boolean add(E e);
简单来说,此方法有三个要素:将新元素添加到list末尾、只能传入指定类型元素、部分list拒接添加null。
但据我所知,JDK中应该是没有原生的"NULL判断"来拒绝null的添加。
2.1.2.实例截图
2.1.3.Null引起的题外话
Collection规范指出,当collection不支持null时,应引发NullPointerException。戳》》》菜鸟教程Interface Collection<E> 但是,此操作不是必须的,所以看自己选择。
2.2.add(int index, E element)
2.2.1.源码
/*** Inserts the specified element at the specified position in this list* (optional operation). * 在此列表中的指定位置插入指定的元素(可选操作)。* * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).* 将当前位于该位置的元素(如果有)和任何后续元素向右移动(在其索引中添加一个)。** @param index index at which the specified element is to be inserted* @param element element to be inserted* @throws UnsupportedOperationException if the <tt>add</tt> operation* is not supported by this list* @throws ClassCastException if the class of the specified element* prevents it from being added to this list* @throws NullPointerException if the specified element is null and* this list does not permit null elements* @throws IllegalArgumentException if some property of the specified* element prevents it from being added to this list* @throws IndexOutOfBoundsException if the index is out of range* (<tt>index < 0 || index > size()</tt>)*/void add(int index, E element);
2.2.2.示例截图
当①执行时,下标为1的元素A,将原本list中的元素2,3,4,5都给向后移动了。
当②和③执行时,新添加了元素B,但是出现了IndexOutOfBoundsException(下标越界异常),由此可以看出,当使用 add(int index, E element) 时,需要满足两个条件之一:当前下标已经存在元素,或当前下标的前一个位置存在元素。
==》当list中没有元素时,add的下标就需要从0开始,否则也会出现下标越界问题。
2.2.3. add()引起IndexOutOfBoundsException简介
待补充...
三、addAll()方法介绍
3.1.addAll
3.1.1.源码
/*** 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). * 按照指定集合的迭代器返回的顺序,将指定集合中的所有元素追加到此列表的末尾(可选操作)。** The behavior of this operation is undefined if the specified collection is * modified while the operation is in progress. (Note that this will occur if the* specified collection is this list, and it's nonempty.)* 如果在操作进行期间修改了指定的集合,则此操作的行为是未定义的。* (请注意,如果指定的集合就是这个list,它不是空的。)** @param c collection containing elements to be added to this list* @return <tt>true</tt> if this list changed as a result of the call* @throws UnsupportedOperationException if the <tt>addAll</tt> operation* is not supported by this list* @throws ClassCastException if the class of an element of the specified* collection prevents it from being added to this list* @throws NullPointerException if the specified collection contains one* or more null elements and this list does not permit null* elements, or if the specified collection is null* @throws IllegalArgumentException if some property of an element of the* specified collection prevents it from being added to this list* @see #add(Object)*/boolean addAll(Collection<? extends E> c);
由上述源码可知,addAll的两个方法与add的使用大体相同,都是在原list内添加或在原list指定位置添加,同时下标取值范围 不能符合(index < 0 || index > size()),不过
1、add添加的是元素,addAll添加的是 Collection<? extends E> c
2、add使用时,部分list中不允许添加null,但还是能在list中添加null元素的;
addAll则不允许添加null,但可以添加null元素
3.1.2.代码示例
package com.task.test;import java.util.ArrayList;
import java.util.List;public class T1 {public static void main(String[] args) {List<String> list1 = new ArrayList<>();new T1().returnList1(list1);List<String> list2 = new ArrayList<>();new T1().returnList2(list2);List<String> list3 = new ArrayList<>();new T1().returnList3(list3);List<String> list4 = new ArrayList<>();List<String> list5 = null;List<String> listAll = new ArrayList<>();listAll.addAll(list1);System.out.println("1:listAll = " + listAll);listAll.addAll(list2);System.out.println("2:listAll = " + listAll);listAll.addAll(5,list3);System.out.println("3:listAll = " + listAll);listAll.addAll(list4);System.out.println("4:listAll = " + listAll);listAll.addAll(list5);System.out.println("5:listAll = " + listAll);}public void returnList1(List list){list.add("A1");list.add("B1");list.add("C1");list.add("D1");list.add("E1");list.add("F1");list.add("G1");}public void returnList2(List list){list.add("A2");list.add("B2");list.add("C2");list.add("D2");list.add("E2");list.add("F2");list.add("G2");}public void returnList3(List list){list.add(null);}
}
3.1.3.运行截图
关于list4 和 list5 的区别,可以查看》》》ArrayList初始化的小知识
四、set(int index,E element)
4.1.元素替换方法:set(int index,E element)
4.1.1.源码
/*** Replaces the element at the specified position in this list with the* specified element (optional operation).* 将此列表中指定位置的元素替换为指定的元素(可选操作)。** @param index index of the element to replace* @param element element to be stored at the specified position* @return the element previously at the specified position* @throws UnsupportedOperationException if the <tt>set</tt> operation* is not supported by this list* @throws ClassCastException if the class of the specified element* prevents it from being added to this list* @throws NullPointerException if the specified element is null and* this list does not permit null elements* @throws IllegalArgumentException if some property of the specified* element prevents it from being added to this list* @throws IndexOutOfBoundsException if the index is out of range* (index < 0 || index >= size())*/E set(int index, E element);
1、这是个元素替换方法。
2、使用set时的下标index 取值范围不能在(index < 0 || index >= size())之间。