吉林快-三平台出租Q1446595067解析源码的方法>>> list.add("hello");
该方法涉及到的方法被我复制到了一个类中,至于解释,全在注释上。初次解析,别喷我!!!
如有不足望评论,随时补充。
package com.nc.sourceCode;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SourceCodeTest_02<E> {
public static void main(String[] args) {
user us=new user();
System.out.println(us.getA());
List<String> list1=new ArrayList<String>();
System.out.println("开始添加");
// for (Integer i = 0; i <2100000000; i++) {
// list1.add(i.toString());
// }
System.out.println(list1);
list1.add("我就不信了");
int a1=2147483647;
int a2=a1+1000;
System.out.println(a1+" "+a2);
System.out.println("=====Arrays.copyOf()方法测试====");
String [] str={"7","6","5","4","3","2","1"};
String[] copyOf = Arrays.copyOf(str, 3);
for (String string : copyOf) {
System.out.print(string+" ");
}
System.out.println();
System.out.println(-6-200);
System.out.println("======位运算符测试======");
int a=109;
System.out.println("运算前:"+a);
System.out.println(a>>1);
System.out.println(a+(a>>1));
System.out.println(a+a>>1);
System.out.println("运算后:"+a);
System.out.println("=======list集合测试=========");
List<String> list=new ArrayList<String>();
list.add("我去哦");
int size2 = list.size();
System.out.println("list.size()="+size2);
System.out.println(list);
System.out.println(list.get(0));
System.out.println("=====数学测试======");
int max = Math.max(10, 1);
System.out.println(max);
System.out.println(Integer.MAX_VALUE);
}
transient Object[] elementData;
private int size;
public boolean add(E e) {
ensureCapacityInternal(size + 1); //进行数组长度处理
elementData[size++] = e;//实际添加元素的操作
return true;
}
private void ensureCapacityInternal(int minCapacity) {//类似中转站
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
//返回一个最大值
private static int calculateCapacity(Object[] elementData, int minCapacity) {//判断数组是否为空(也就是看下是否是第一次集合中添加数据),初始长度为10,否则则就返回数组的下一位元素的下标,也就是上面的size+1
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);//返回两个值中的大的那个
}
return minCapacity;
}
protected transient int modCount = 0;
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
if (minCapacity - elementData.length > 0)//超出object[]长度时
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;//2147483647-8
private void grow(int c) {//在这个方法中进行长度判断,如果超出当前数组长度,则扩展长度为当前长度的一半或者扩展长度为1
int a = elementData.length;//获取数组长度
int b = a + (a >> 1);//类似于除以2,如果为单数,则减一后除以2
if (b - c < 0)
b = c;
if (b - MAX_ARRAY_SIZE > 0)//基本不可能发生,谁会闲着没事往一个list集合里面放21亿多个数据呢
b = hugeCapacity(c);
elementData = Arrays.copyOf(elementData, b);//拷贝elementData数组元素从0位到c位至elementData数组
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
public static <T> T[] copyOf(T[] original, int newLength) {//实际扩产长度的方法,先创建一个指定长度的数组,然后将原数组拷贝到新的数组中并返回新数组
return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);//创建一个指定类型和长度的数组
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));//将original源数组中从第0位开始到Math位结束的元素拷贝到copy数组中从copy数组的地0位开始拷贝
return copy;
}
}
原文地址:http://blog.51cto.com/13880707/2147968
时间: 2024-10-09 20:51:32