动态数组:java.lang.System下的arraycopy和java.util.Arrays.copyOf方法
java.lang.System下的arraycopy和java.util.Arrays.copyOf方法
(1) java.lang.System.arraycopy
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
* @param src the source array. * @param srcPos starting position in the source array. * @param dest the destination array. * @param destPos starting position in the destination data. * @param length the number of array elements to be copied. * @exception IndexOutOfBoundsException if copying would cause * access of data outside array bounds. * @exception ArrayStoreException if an element in the <code>src</code> * array could not be stored into the <code>dest</code> array * because of a type mismatch. * @exception NullPointerException if either <code>src</code> or * <code>dest</code> is <code>null</code>. [b](2) java.util.Arrays.copyOf [/b] 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)); return copy; }
package com.yilong.array.copyof; public class ArrayCopyOf { public static void main(String[] args) { String[] oldStr = new String[6]; setValues(oldStr); String[] newStr = null; newStr = java.util.Arrays.copyOf(oldStr, 6); //System.arraycopy(oldStr, 0, newStr, 0, oldStr.length); // 会报错NullPointerException ,如果改为newStr = new String[5], // 那么调用Arrays.copyOf 方法不会报错,而调用System.arraycopy 方法 // 则回报IndexOutOfBoundsException print(oldStr); print(newStr); System.out.println(oldStr.length); System.out.println(newStr.length); } private static void print(String[] newStr) { // TODO Auto-generated method stub for (int i = 0; i < newStr.length; i++) { System.out.print(newStr[i] + " : "); } System.out.println(); } private static void setValues(String[] oldStr) { // TODO Auto-gemmnerated method stub for (int i = 0; i < oldStr.length; i++) { oldStr[i] = "str " + i; } } }
public static byte[] copyOf(byte[] original, int newLength) { byte[] copy = new byte[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; } public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
private transient Object[] elementData;// 为什么要设置成transient? 不想被序列化? public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); this.elementData = new Object[initialCapacity]; } public ArrayList() { this(10); }
public boolean add(E e) { ensureCapacity(size + 1); // Increments modCount!! elementData[size++] = e; return true; } 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); } }
public Object[] toArray() { return Arrays.copyOf(elementData, size); } public <T> T[] toArray(T[] a) { if (a.length < size) // Make a new array of a's runtime type, but my contents: return (T[]) Arrays.copyOf(elementData, size, a.getClass()); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; }
private String[] buffer = new String[100]; public final void push(String goods) { if (point + 2 > buffer.length) { // System.arraycopy is better than Arrays.copyOf // buffer = Arrays.copyOf(buffer, buffer.length + INCREMENT); String[] newBuffer = new String[(buffer.length * 3) / 2 + 1]; System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); buffer = newBuffer; } point++; buffer[point] = goods; System.out.println("buffer size ========================" + buffer.length); System.out.println("point ========================" + point); }
Exception in thread "****producer***" java.lang.ArrayIndexOutOfBoundsException: 100