1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package java.util; 18 19import java.lang.reflect.Array; 20 21/** 22 * An array-backed list that exposes its array. 23 * 24 * @hide 25 */ 26public class UnsafeArrayList<T> extends AbstractList<T> { 27 private final Class<T> elementType; 28 private T[] array; 29 private int size; 30 31 public UnsafeArrayList(Class<T> elementType, int initialCapacity) { 32 this.array = (T[]) Array.newInstance(elementType, initialCapacity); 33 this.elementType = elementType; 34 } 35 36 @Override public boolean add(T element) { 37 if (size == array.length) { 38 T[] newArray = (T[]) Array.newInstance(elementType, size * 2); 39 System.arraycopy(array, 0, newArray, 0, size); 40 array = newArray; 41 } 42 array[size++] = element; 43 ++modCount; 44 return true; 45 } 46 47 public T[] array() { 48 return array; 49 } 50 51 public T get(int i) { 52 return array[i]; 53 } 54 55 public int size() { 56 return size; 57 } 58} 59