ListView的addAll方法

add是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素addAll是传入一个List,将此List中的所有元素加入到当前List中,也就是当前List会增加的元素个数为传入的List的大小。

1.add源代码:

1     //add源代码:  
2       
3         public boolean add(E e) {  
4             ensureCapacityInternal(size + 1);   
5             elementData[size++] = e;  
6             return true;  
7         }  
8       
9     
addAll源代码:

2.将Collection c内的数据插入ArrayList中

 1 //addAll源代码:  
 2       
 3        //将Collection c内的数据插入ArrayList中  
 4        public boolean addAll(Collection<? extends E> c) {  
 5             Object[] a = c.toArray();  
 6             int numNew = a.length;  
 7             ensureCapacityInternal(size + numNew);  // Increments modCount  
 8             System.arraycopy(a, 0, elementData, size, numNew);  
 9             size += numNew;  
10             return numNew != 0;  
11         }  
12       
13         

3.将Collection c中的数据插入到ArrayList的指定位置

 1 //将Collection c中的数据插入到ArrayList的指定位置  
 2         public boolean addAll(int index, Collection<? extends E> c) {  
 3             rangeCheckForAdd(index);  
 4       
 5             Object[] a = c.toArray();  
 6             int numNew = a.length;  
 7             ensureCapacityInternal(size + numNew);  // Increments modCount  
 8       
 9             int numMoved = size - index;  
10             if (numMoved > 0)  
11                 System.arraycopy(elementData, index, elementData, index + numNew,  
12                                  numMoved);  
13       
14             System.arraycopy(a, 0, elementData, index, numNew);  
15             size += numNew;  
16             return numNew != 0;  
17         }  

使用时直接使用ArrayList的对象调用即可

1 ArrayList<News> newslist = mnewsdetail.data.news;//ArrayList对象
2 
3 1.ArrayList<News> news = mnewsdetail.data.news;
4 newslist.addAll(news);//默认将news 的数据追加在newslist的后面
5 mnewsListAdapter.notifyDataSetChanged();//刷新列表
6 
7 2.ArrayList<News> news = mnewsdetail.data.news;
8 newslist.addAll(0,news);//默认将news 的数据插入在newslist的0的位置上,
9 mnewsListAdapter.notifyDataSetChanged();//刷新列表
原文地址:https://www.cnblogs.com/wangying222/p/5301812.html