Int Vector : 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 




Int Vector
       

/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 
 * $Id: IntVector.java,v 1.1.1.1 2004/05/09 16:57:53 vlad_r Exp $
 */

// ----------------------------------------------------------------------------
/**
 @author Vlad Roubtsov, (C) 2001
 */
public
final class IntVector implements Cloneable
{
    // public: ................................................................
    
    public IntVector ()
    {
        this (5);
    }
    
    public IntVector (final int initCapacity)
    {
        m_values = new int [initCapacity];
    }
    
    // ACCESSORS:
    
    public int get (final int index)
    {
        if (index > m_size - 1)
            throw new IndexOutOfBoundsException ("get[" + index + "] on vector of size " + m_size);
                
        return m_values [index];
    }
    
    public int [] values ()
    {
        if (m_size == 0)
            return new int[0];
        else
        {
            final int size = m_size;
            final int [] result = new int [size];
            
            if (size < COPY_THRESHOLD)
            {
                for (int i = 0; i < size; ++ iresult [i= m_values [i];
            }
            else
            {
                System.arraycopy (m_values, 0, result, 0, size);
            }
            
            return result;
        }
    }
    
    public int size ()
    {
        return m_size;
    }
    
    // Cloneable:
    
    /**
     * Performs deep copy.
     */
    public Object clone ()
    {
        try
        {
            final IntVector _clone = (IntVectorsuper.clone ();
            
            // deep clone:
            if (m_size < COPY_THRESHOLD)
            {
                _clone.m_values = new int [m_values.length];
                final int [] _clone_values = _clone.m_values;
                for (int i = 0; i < m_size; ++ i_clone_values [i= m_values [i];
            }
            else
            {
                _clone.m_values = (int []) m_values.clone ();
            }
            
            return _clone;
        }
        catch (CloneNotSupportedException e)
        {
            throw new InternalError (e.toString ());
        }
    }
    
    public String toString ()
    {
        final StringBuffer s = new StringBuffer (super.toString() ", size " + m_size + ": ");
        for (int i = 0; i < m_size; ++ i)
        {
            if (i > 0s.append (", ");
            s.append (m_values [i]);
        }
        
        return s.toString ();
    }
    
    // MUTATORS:
    
    public int set (final int index, final int value)
    {
        if (index > m_size - 1)
            throw new IndexOutOfBoundsException ("get[" + index + "] on vector of size " + m_size);
        
        final int current_value = m_values [index];
        m_values [index= value;
        
        return current_value;
    }
    
    public void add (final int value)
    {
        final int capacity = m_values.length;
        if (capacity == m_size)
        {
            final int [] values = new int [(capacity << 1)];
            if (capacity < COPY_THRESHOLD)
            {
                for (int i = 0; i < capacity; ++ ivalues [i= m_values [i];
            }
            else
            {
                System.arraycopy (m_values, 0, values, 0, capacity);
            }
            
            m_values = values;
        }
        
        m_values [m_size ++= value;
    }    
    
    // protected: .............................................................

    // package: ...............................................................
    
    // private: ...............................................................
    
    
    private int [] m_values; // never null
    private int m_size;
    
    private static final int COPY_THRESHOLD = 10;

// end of class
// ----------------------------------------------------------------------------

   
    
    
    
    
    
    
  














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.Fast Array
20.Extensible vector of bytes
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.