Growable int[] : 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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Book
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » Collections Data Structure » Auto Growth ArrayScreenshots 
Growable int[]
         


/*
 * Copyright (c) 1998 - 2005 Versant Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Versant Corporation - initial API and implementation
 */

/**
 * Growable int[]. This is based com.sosnoski.util.array.IntArray from
 * Sosnoski Software Solutions, Inc.
 */
public final class IntArray {

    private int[] buf;
    private int size;

    public IntArray() {
        this(64);
    }

    public IntArray(int capacity) {
        buf = new int[capacity];
    }

    public int size() {
        return size;
    }

    private void ensureCapacity(int len) {
        if (size + len > buf.length) {
            int n = buf.length * 1;
            if (size + len > n) {
                n = size + len;
            }
            int[] a = new int[n];
            System.arraycopy(buf, 0, a, 0, size);
            buf = a;
        }
    }

    public void add(int v) {
        ensureCapacity(size + 1);
        buf[size++= v;
    }

    /**
     * Add a value at a specified index in the array.
     */
    public void add(int index, int value) {
        ensureCapacity(size + 1);
        if (index == size) {
            buf[size++= value;
        else {
            System.arraycopy(buf, index, buf, index + 1, size - index);
            buf[index= value;
        }
    }

    /**
     * Constructs and returns a simple array containing the same data as held
     * in this growable array.
     */
    public int[] toArray() {
        int[] a = new int[size];
        System.arraycopy(buf, 0, a, 0, size);
        return a;
    }

    public void clear() {
        size = 0;
    }

    /**
     * Retrieve the value present at an index position in the array.
     */
    public int get(int index) {
        return buf[index];
    }

}

   
    
    
    
    
    
    
    
    
  
Related examples in the same category
1.Your own auto-growth Array
2.Long Vector
3.Int Vector (from java-objects-database)
4.ArrayList of int primitives
5.ArrayList of long primitives
6.ArrayList of short primitives
7.ArrayList of double primitives
8.ArrayList of boolean primitives
9.ArrayList of char primitives
10.ArrayList of byte primitives
11.Growable String array with type specific access methods.
12.Auto Size Array
13.Dynamic Int Array
14.Dynamic Long Array
15.Int Array
16.Int Array List
17.ArrayList of float primitives
18.Fast Array
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
w___w_w___.___ja_v___a___2_s__.c__o___m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.