java list add 方法理解

来源:互联网 发布:快拍王软件下载 编辑:程序博客网 时间:2024/04/30 12:02

java list add 方法理解

void add(int index, E element)
在列表的指定位置插入指定元素(可选操作)。将当前处于该位置的元素(如果有的话)和所有后续元素向右移动(在其索引中加 1)。

参数:
index - 要在其中插入指定元素处的索引
element - 要插入的元素
抛出:
UnsupportedOperationException - 如果列表不支持 add 操作
ClassCastException - 如果指定元素的类不允许它添加到此列表
NullPointerException - 如果指定的元素为 null,并且此列表不允许 null 元素
IllegalArgumentException - 如果指定元素的某些属性不允许它添加到此列表
IndexOutOfBoundsException - 如果索引超出范围 (index < 0 || index > size())
举例:

for(int i=0;i<this.pagination.getList().size();i++){      Seed s = null;      s = (Seed)this.pagination.getList().get(i);      int count = mySeedMng.countSeedsBySeedId(s.getId());      //运用了同一个引用,所以修改s的数据后,相应的list的数据也自动修改了      s.setViewerNum(count); }
0 0
原创粉丝点击