Fast Array : Auto Growth Array « Collections Data Structure « Java






Fast Array

       

/*
 * Copyright 2003-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.AbstractList;
import java.util.Collection;
import java.util.List;

public class FastArray implements Cloneable {
    private Object[] data;
    public int size;
    public static final FastArray EMPTY_LIST = new FastArray(0);

    public FastArray(int initialCapacity) {
        data = new Object[initialCapacity];
    }

    public FastArray() {
       this (8);
    }

    public FastArray(Collection c) {
        this (c.toArray());
    }

    public FastArray(Object[] objects) {
        data = objects;
        size = objects.length;
    }

    public Object get(int index) {
        return data [index];
    }

    public void add(Object o) {
        if (size == data.length) {
            Object [] newData = new Object[size == 0 ? 8 : size*2];
            System.arraycopy(data, 0, newData, 0, size);
            data = newData;
        }
        data [size++] = o;
    }

    public void set(int index, Object o) {
        data [index] = o;
    }

    public int size() {
        return size;
    }

    public void clear() {
        data = new Object[data.length];
        size = 0;
    }

    public void addAll(FastArray newData) {
        addAll(newData.data, newData.size);
    }

    public void addAll(Object [] newData, int size) {
        if (size == 0)
          return;
        final int newSize = this.size + size;
        if (newSize > data.length) {
            Object nd [] = new Object [newSize];
            System.arraycopy(data, 0, nd, 0, this.size);
            data = nd;
        }
        System.arraycopy(newData, 0, data, this.size, size);
        this.size = newSize;
    }

    public FastArray copy() {
        final Object[] newData = new Object[size];
        System.arraycopy(data, 0, newData, 0, size);
        return new FastArray(newData);
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void addAll(List coll) {
        final Object[] newData = coll.toArray();
        addAll(newData, newData.length);
    }

    public void remove(int index) {
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(data, index+1, data, index, numMoved);
        data[--size] = null;
    }

    public List toList () {
        return new AbstractList() {

            public Object get(int index) {
                return FastArray.this.get(index);
            }

            public int size() {
                return size;
            }
        };
    }

    public Object[] getArray() {
        return data;
    }

    public String toString() {
        if (size() == 0) return "[]";
        return toList().toString();
    }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Growable int[]
2.Your own auto-growth Array
3.Long Vector
4.Int Vector (from java-objects-database)
5.ArrayList of int primitives
6.ArrayList of long primitives
7.ArrayList of short primitives
8.ArrayList of double primitives
9.ArrayList of boolean primitives
10.ArrayList of char primitives
11.ArrayList of byte primitives
12.Growable String array with type specific access methods.
13.Auto Size Array
14.Dynamic Int Array
15.Dynamic Long Array
16.Int Array
17.Int Array List
18.ArrayList of float primitives
19.Extensible vector of bytes
20.Int Vector
21.A two dimensional Vector
22.Lazy List creation based on ArrayList
23.Append the given Object to the given array
24.Adds all the elements of the given arrays into a new array.
25.Simple object pool
26.A variable length Double Array: expanding and contracting its internal storage array as elements are added and removed.
27.Append item to array
28.A growable array of bytes
29.Doubles the size of an array
30.Adds the object to the array.
31.Concatenate arrays
32.Double List