public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
private transient Object[] elementData;
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
//....................
}
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @exception IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this(10);
}
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
/**
* Increases the capacity of this <tt>ArrayList</tt> instance, if
* necessary, to ensure that it can hold at least the number of elements
* specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
/**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If the list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
* (if such an element exists). Returns <tt>true</tt> if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return <tt>true</tt> if this list contained the specified element
*/
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
分享到:
相关推荐
import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; /** * @Author Ziph * @date 2020/2/19 * @Email mylifes1110@163.com */ public class TestDemo implemen
一个C++(Ubuntu16.04+QT5.9.1)通过JNI,调用JAVA类及方法的示例。通过JNI传递和返回多种类型的参数,boolean ,int,String,ArrayList,ArrayList嵌套ArrayList<ArrayList<String>>等。
集合ArrayList测试集合ArrayList测试集合ArrayList测试集合ArrayList测试集合ArrayList测试集合ArrayList测试
ListView显示单列ArrayList_demo 在 Android 开发中,ListView 是一种常用的 UI 组件,用于显示列表数据。今天,我们将探索如何使用 ArrayList 将数据显示在 ListView 中。 标题解释 标题 "ListView 显示单列 ...
在jni中操作arraylist对象,然后添加一个int型数据进去
ArrayList常用的方法如下: Add 添加到 ArrayList 的結尾處 Remove 從 ArrayList 中移除特定對象的第一個匹配項 RemoveAt 移除 ArrayList 的指定索引處的元素 Insert 將元素插入 ArrayList 的指定索引處 ...
C# Array和ArrayList,List区别
C#数据结构之Array、ArrayList、Hashtable、Dictionary C#中有多种数据结构可以用来存储和管理数据,今天我们将讨论四种常用的数据结构:Array、ArrayList、Hashtable和Dictionary。这些数据结构都是_Collections_...
浅析ArrayList内部实现 资源源于不但搜索,自由源于不但努力
ArrayList转化为DataTable数据加载转换方便程序的灵活运用!
day14-ArrayList集合 1.ArrayList 1.1ArrayList类概述【理解】 什么是集合 提供一种存储空间可变的存储模型,存储的数据容量可以发生改变 ArrayList集合的特点 底层是数组实现的,长度可以变化 泛型的使用 ...
使用对象ArrayList填充DataGrid,C#源代码ArrayList MyList = new ArrayList();
自定义实现的ArrayList数据结构,有大量注释以及编写思路,帮助新手用Java语言来实现数据结构
ArrayList。 首先,它不是静态的,编译时每一维度的元素个数不用指定,系统默认元素个数为16,当元素增多并即将大于16时,它会增倍扩容到32,依次规律增长,变小时,相反处理。 其次,元素类型是弱类型,object...
使用数组列表ArrayList填充ListBox
ASP .NET - ArrayList对象ASP .NET - ArrayList对象ASP .NET - ArrayList对象ASP .NET - ArrayList对象
ArrayList数据批量添加数据,供新手参考
C语言版的ArrayList,具有ArrayList的基本方法增加、插入、删除、自动扩容等。
java.util包中的ArrayList很常用,参考jdk源码中的ArrayList.java,写了一个c版的ArrayList,目前仅坚持 char *(字符串) 和 int (整型).
ArrayList集合与HashMap的扩容原来 ArrayList扩容原理 ArrayList集合的底层是数组,特点是查询快增删慢。当创建一个集合时,ArrayList集合的空参构造底层数组默认长度为 0,即new Object[0]。当添加第一个元素时,...