arraylist编程源码(arraylist源代码)
本文目录一览:
- 1、arraylist源代码问题
- 2、java中ArrayList的源代码是什么
- 3、ArrayList源码的问题
- 4、ArrayList源码中的toArray方法中的这块代码是什么意思?
- 5、关于JDK中的ArrayList的源码
- 6、一开始学习java有必要看源代码吗?诸如Arraylist,Linkedlist的这些源代码
arraylist源代码问题
protected AbstractList()唯一的构造方法。(由子类构造方法调用,通常是隐式的。)
抽象的类不是没有构造方法,只是不能实例化
java中构造器的调用是级联调用的.
一真会调用到Object
原因:
根据替代性原理,子类对象必须能够替代父类对象.因此在构造子类对象时,要首先构造一个父类对象,然后在父类对象的基础上进一步构造子类对象.也就是说,java中的子类对象中都隐含着一个父类对象,子类对象是在父类对象的基础上进一步雕琢成的.
书上原话.我也不是很明白.反正知道怎么调用就行了.
java中ArrayList的源代码是什么
package java.util;
public class ArrayListE extends AbstractListE
implements ListE, 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 E[] 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 = (E[])new Object[initialCapacity];
}
public ArrayList() {
this(10);
}
public ArrayList(Collection? extends E c) {
size = c.size();
// Allow 10% room for growth
int capacity = (int) Math.min((size*110L)/100, Integer.MAX_VALUE);
elementData = (E[]) c.toArray(new Object[capacity]);
}
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size oldCapacity) {
Object oldData[] = elementData;
elementData = (E[])new Object[size];
System.arraycopy(oldData, 0, elementData, 0, size);
}
}
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;
elementData = (E[])new Object[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, size);
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean contains(Object elem) {
return indexOf(elem) = 0;
}
public int indexOf(Object elem) {
if (elem == null) {
for (int i = 0; i size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i size; i++)
if (elem.equals(elementData[i]))
return i;
}
return -1;
}
public int lastIndexOf(Object elem) {
if (elem == null) {
for (int i = size-1; i = 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i = 0; i--)
if (elem.equals(elementData[i]))
return i;
}
return -1;
}
public Object clone() {
try {
ArrayListE v = (ArrayListE) super.clone();
v.elementData = (E[])new Object[size];
System.arraycopy(elementData, 0, v.elementData, 0, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
public Object[] toArray() {
Object[] result = new Object[size];
System.arraycopy(elementData, 0, result, 0, size);
return result;
}
public T T[] toArray(T[] a) {
if (a.length size)
a = (T[])java.lang.reflect.Array.
newInstance(a.getClass().getComponentType(), size);
System.arraycopy(elementData, 0, a, 0, size);
if (a.length size)
a[size] = null;
return a;
}
// Positional Access Operations
public E get(int index) {
RangeCheck(index);
return elementData[index];
}
public E set(int index, E element) {
RangeCheck(index);
E oldValue = elementData[index];
elementData[index] = element;
return oldValue;
}
public boolean add(E o) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = o;
return true;
}
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++;
}
public E remove(int index) {
RangeCheck(index);
modCount++;
E oldValue = elementData[index];
int numMoved = size - index - 1;
if (numMoved 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
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;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
}
public void clear() {
modCount++;
// Let gc do its work
for (int i = 0; i size; i++)
elementData[i] = null;
size = 0;
}
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;
}
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;
}
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
int newSize = size - (toIndex-fromIndex);
while (size != newSize)
elementData[--size] = null;
}
private void RangeCheck(int index) {
if (index = size)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
int expectedModCount = modCount;
// Write out element count, and any hidden stuff
s.defaultWriteObject();
// Write out array length
s.writeInt(elementData.length);
// Write out all elements in the proper order.
for (int i=0; isize; i++)
s.writeObject(elementData[i]);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in array length and allocate array
int arrayLength = s.readInt();
Object[] a = elementData = (E[])new Object[arrayLength];
// Read in all elements in the proper order.
for (int i=0; isize; i++)
a[i] = s.readObject();
}
}
ArrayList源码的问题
兄弟 你没有仔细阅读源代码吧!
private File(String child, File parent) {
assert parent.path != null;
assert (!parent.path.equals(""));
this.path = fs.resolve(parent.path, child);
this.prefixLength = parent.prefixLength;
}
这是源代码其中的一个构造函数 当然可以在File类中用new File(s, this)
this就是指代File对象的 而File对象本身都有私有的构造函数
ArrayList源码中的toArray方法中的这块代码是什么意思?
pIf the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to ttnull/tt. (This is useful in determining the length of the list ionly/i if the caller knows that the list does not contain any null elements.)
这个是官方的注释,意思是如果你的这个参数a的长度比你的list的长度要长的话,就会在把arrayList中的元素复制完后加一个null,用于标记数组结尾。list的长度是size,所以a[size]要设置为null,做为一个结尾的符号
关于JDK中的ArrayList的源码
这个称之为面向接口编程ArrayList虽然是一个具体的类,按照ArrayList myList=new ArrayList();的确可以生成一个myList对象,而且编译也不会报错。但是在实际开发中时不采用这样的方式即实际开发时都是 接口名 xxx=new 接口某实现类()。这样便于
1。便于程序规范化设计
2。便与团队协同开发
3。便于转换为组件
4。方便的代码复用,无需了解技术细节。
一开始学习java有必要看源代码吗?诸如Arraylist,Linkedlist的这些源代码
你好,看源码是可以帮助你以后写代码的。如果你是刚开始学,就没有必要看那些东西。但是你要是有能力的话,看看还是很有帮助的,你说的那几个类,等你学习到了,最好还是看看,可以加深你对他们的理解。