java中的ArrayList中的addAll方法

duzhonghua 2008-04-08 10:54:28
ArrayList list =new ArrayList ();
list.addAll(index ,Collection);说一下它的用法,及于add()之间的区别和好处


//前面省略
ArrayList listO = new ArrayList ();
listO.add("aa");
listO.add("bb");
ArrayList listT = new ArrayList ();
listT.add("cc");
listT.add("dd");
ArrayList listAll = new ArrayList ();
listAll.addAll(0 , list0);
listAll.addAll(1 , listT);
// 怎么样遍历listAll????
...全文
8963 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
th82007 2011-08-29
  • 打赏
  • 举报
回复
原来如此、、
dracularking 2008-04-08
  • 打赏
  • 举报
回复
火龙果正解
Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices).
  • 打赏
  • 举报
回复
对的,0 表示从第 0 个元素插入,即合部插入到最前面,原本的当然要后移了。
duzhonghua 2008-04-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dracularking 的回复:]
Inserts all of the elements in the specified collection into this list at the specified position
有一点要明确 addAll加进去的不是集合 而是集合中的元素
[/Quote]
呵呵,再问个,那listAll.addAll(index , list0); 0有什么作用啊,就是表示从listAll的什么地方插入???,如果是原来的数据要向后移???
kingssq 2008-04-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dracularking 的回复:]
Inserts all of the elements in the specified collection into this list at the specified position
有一点要明确 addAll加进去的不是集合 而是集合中的元素
[/Quote]
dracularking 2008-04-08
  • 打赏
  • 举报
回复
Inserts all of the elements in the specified collection into this list at the specified position
有一点要明确 addAll加进去的不是集合 而是集合中的元素
dracularking 2008-04-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 duzhonghua 的回复:]
引用 1 楼 dracularking 的回复:
addAll是组加 add是散加 区别是显而易见的
遍历方式不止一种 可以通过获取iterator 或者直接用for each根据类型遍历


Java codeIterator it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}


Java codefor(Object str: list){
System.out.println(str);
}


组加时,我里面还是集合,你用it.next()就打印出来了??
还…
[/Quote]
要不是集合你也不能加啊
直接add是追加 而addAll可以用来insert
不管addAll还是add 操作完成之后还是一个list
该怎么遍历还是怎么遍历 与加不加没有影响
csc1215 2008-04-08
  • 打赏
  • 举报
回复
List里面可以是任何对象,如果不使用泛型取出时要强制转换
csc1215 2008-04-08
  • 打赏
  • 举报
回复
boolean add(E o)
向列表的尾部追加指定的元素
void add(int index, E element)
在列表的指定位置插入指定元素
boolean addAll(Collection<? extends E> c)
追加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序

boolean addAll(int index, Collection<? extends E> c)
将指定 collection 中的所有元素都插入到列表中的指定位置

for(Iterator i=listAll .iterator(); i.hasNext();){
String str= (String)i.next();
}

for (String str :listAll )
System.out.println(str);
}

duzhonghua 2008-04-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dracularking 的回复:]
addAll是组加 add是散加 区别是显而易见的
遍历方式不止一种 可以通过获取iterator 或者直接用for each根据类型遍历


Java codeIterator it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}




Java codefor(Object str: list){
System.out.println(str);
}
[/Quote]

组加时,我里面还是集合,你用it.next()就打印出来了??
还有就是你说散加和组加时,我可以这样写啊,
listAll.add(Collection) ; Collection是个ArrayList
和listAll.addAll(0,Collection)有什么区别的>>??高手指教
dracularking 2008-04-08
  • 打赏
  • 举报
回复
addAll是组加 add是散加 区别是显而易见的
遍历方式不止一种 可以通过获取iterator 或者直接用for each根据类型遍历

Iterator it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}


for(Object str: list){
System.out.println(str);
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧