
正文
ListView的addAll方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
add是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素addAll是传入一个List,将此List中的所有元素加入到当前List中,也就是当前List会增加的元素个数为传入的List的大小。
1.add源代码:
//add源代码: public boolean add(E e) {
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
addAll源代码:
2.将Collection c内的数据插入ArrayList中
//addAll源代码: //将Collection c内的数据插入ArrayList中
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
3.将Collection c中的数据插入到ArrayList的指定位置
//将Collection c中的数据插入到ArrayList的指定位置
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index); Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved); System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
使用时直接使用ArrayList的对象调用即可
ArrayList<News> newslist = mnewsdetail.data.news;//ArrayList对象 1.ArrayList<News> news = mnewsdetail.data.news;
newslist.addAll(news);//默认将news 的数据追加在newslist的后面
mnewsListAdapter.notifyDataSetChanged();//刷新列表 2.ArrayList<News> news = mnewsdetail.data.news;
newslist.addAll(0,news);//默认将news 的数据插入在newslist的0的位置上,
mnewsListAdapter.notifyDataSetChanged();//刷新列表








