Double List : 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 
Double List
        
/* Copyright (C) 2002 Univ. of Massachusetts Amherst, Computer Science Dept.
   This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
   http://www.cs.umass.edu/~mccallum/mallet
   This software is provided under the terms of the Common Public License,
   version 1.0, as published by http://www.opensource.org.  For further
   information, see the file `LICENSE' included with this distribution. */




/**
   A dynamically growable list of doubles.
   
   @author Andrew McCallum <a href="mailto:[email protected]">[email protected]</a>
 */

//package cc.mallet.util;

import java.util.Arrays;
import java.io.*;

public class DoubleList implements Serializable
{
  double[] data;
  int size;

  public DoubleList ()
  {
    this (2);
  }

  // Creates a list of zero size
  public DoubleList (int capacity)
  {
    if (capacity < 2)
      capacity = 2;
    this.data = new double[capacity];
    this.size = 0;
  }

  public DoubleList (int size, double fillValue)
  {
    int capacity = size;
    if (capacity < 2)
      capacity = 2;
    this.data = new double[capacity];
    Arrays.fill (this.data, fillValue);
    this.size = size;
  }
  
  public DoubleList (double[] initialValues, int size)
  {
    this.data = new double[initialValues.length];
    System.arraycopy (initialValues, 0this.data, 0, initialValues.length);
    this.size = size;
  }

  public DoubleList (double[] initialValues)
  {
    this (initialValues, initialValues.length);
  }

  public DoubleList cloneDoubleList ()
  {
    return new DoubleList (data, size);
  }

  public Object clone ()
  {
    return cloneDoubleList ();
  }

  private void growIfNecessary (int index)
  {
    int newDataLength = data.length;
    while (index >= newDataLength) {
      if (newDataLength < 100)
        newDataLength *= 2;
      else
        newDataLength = (newDataLength * 32;
    }
    if (newDataLength != data.length) {
      double[] newData = new double[newDataLength];
      System.arraycopy (data, 0, newData, 0, data.length);
      data = newData;
    }
  }

  public void add (double value)
  {
    growIfNecessary (size);
    data[size++= value;
  }

  public double get (int index)
  {
    if (index >= size)
      throw new IllegalArgumentException ("Index "+index+" out of bounds; size="+size);
    return data[index];
  }

  public void set (int index, double value)
  {
    growIfNecessary (index);
    data[index= value;
    if (index >= size)
      size = index+1;
  }

  // Serialization 
    
  private static final long serialVersionUID = 1;
  private static final int CURRENT_SERIAL_VERSION = 0;
  
  private void writeObject (ObjectOutputStream outthrows IOException {
    out.writeInt (CURRENT_SERIAL_VERSION);
    int size = data.length;
    out.writeInt(size);
    for (int i=1; i<size; i++) {
      out.writeDouble(data[i]);
    }
    out.writeInt(this.size);
  }
  
  private void readObject (ObjectInputStream inthrows IOException, ClassNotFoundException {
    int version = in.readInt ();
    int len = in.readInt();
    data = new double[len];
    for (int i = 1; i<len; i++) {
      data[i= in.readDouble();
    }
    size = in.readInt();
  }

  
}

   
    
    
    
    
    
    
    
  
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.Int Vector
22.A two dimensional Vector
23.Lazy List creation based on ArrayList
24.Append the given Object to the given array
25.Adds all the elements of the given arrays into a new array.
26.Simple object pool
27.A variable length Double Array: expanding and contracting its internal storage array as elements are added and removed.
28.Append item to array
29.A growable array of bytes
30.Doubles the size of an array
31.Adds the object to the array.
32.Concatenate arrays
w__w_w__.__j__ava_2_s__.___c__om___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.