Java ArrayList添加item实现,之后再补充
来源:互联网 发布:电脑360数据恢复大师 编辑:程序博客网 时间:2024/06/09 16:54
ArrayList提供了add(E e)、add(int index, E element)、addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)这些添加元素的方法。
具体实现如下:
// 将指定的元素添加到此列表的尾部。 public boolean add(E e) { ensureCapacity(size + 1); elementData[size++] = e; return true; } // 将指定的元素插入此列表中的指定位置。 // 如果当前位置有元素,则向右移动当前位于该位置的元素以及所有后续元素(将其索引加1)。 public void add(int index, E element) { if (index > size || index < 0) throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); // 如果数组长度不足,将进行扩容。 ensureCapacity(size+1); // Increments modCount!! // 将 elementData中从Index位置开始、长度为size-index的元素, // 拷贝到从下标为index+1位置开始的新的elementData数组中。 // 即将当前位于该位置的元素以及所有后续元素右移一个位置。 System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; } // 按照指定collection的迭代器所返回的元素顺序,将该collection中的所有元素添加到此列表的尾部。 public boolean addAll(Collection<? extends E> c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacity(size + numNew); // Increments modCount System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0; } // 从指定的位置开始,将指定collection中的所有元素插入到此列表中。 public boolean addAll(int index, Collection<? extends E> c) { if (index > size || index < 0) throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); Object[] a = c.toArray(); int numNew = a.length; ensureCapacity(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; }
0 0
- Java ArrayList添加item实现,之后再补充
- JAVA中native方法调用,之后再补充
- java ArrayList补充——subList
- 4.ListView的Item中添加按钮之后OnItemClick失效
- listview添加头之后,点击item错位问题
- java Combobox,循环添加Item
- java ArrayList无限添加元素
- ArrayList Java实现
- java 之ArrayList实现
- 实现Java中的ArrayList
- ArrayList底层实现(JAVA)
- java ArrayList 的实现
- java ArrayList实现原理
- java ArrayList源代码实现
- JAVA--自己实现ArrayList
- ArrayList Java实现
- Java Arraylist实现原理.
- TV中RecyclerView添加item的点击事件和删除item之后获取焦点解决
- 169.View the Exhibit and examine the description for the PRODUCTS and SALES table.
- 纵表与横表互转的SQL
- TCP和UDP的区别于联系,Scoket连接和HTTP连接的区别
- windows安装并配置apache https 网站 &发布企业级iOS APP(enterprise)
- 自定义SeekBar(简单点的)
- Java ArrayList添加item实现,之后再补充
- 史上最全正则
- 物联网 :概念
- 18 API-集合(Map(Map_HashMap_TreeMap),Collections工具类,集合(C+M)大总结)
- Glide
- hdu2594(kmp)
- Linux定时任务Crontab详解
- HTTP协议的特点,关于HTTP请求GET和POST的区别
- Nginx的 UDP 负载均衡