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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Auto Growth Array 




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 == : 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, 0this.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[--sizenull;
    }

    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() == 0return "[]";
        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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.