ArrayList of boolean primitives : 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 




ArrayList of boolean primitives
         
// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.


import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.IOException;

/**
 * ArrayList of boolean primitives.
 */
public class BooleanArrayList implements Serializable {

  private boolean[] array;
  private int size;

  public static int initialCapacity = 10;

  /**
   * Constructs an empty list with an initial capacity.
   */
  public BooleanArrayList() {
    this(initialCapacity);
  }

  /**
   * Constructs an empty list with the specified initial capacity.
   */
  public BooleanArrayList(int initialCapacity) {
    if (initialCapacity < 0) {
      throw new IllegalArgumentException("Capacity can't be negative: " + initialCapacity);
    }
    array = new boolean[initialCapacity];
    size = 0;
  }

  /**
   * Constructs a list containing the elements of the specified array.
   * The list instance has an initial capacity of 110% the size of the specified array.
   */
  public BooleanArrayList(boolean[] data) {
    array = new boolean[(int) (data.length * 1.11];
    size = data.length;
    System.arraycopy(data, 0, array, 0, size);
  }

  // ---------------------------------------------------------------- conversion

  /**
   * Returns an array containing all of the elements in this list in the correct order.
   */
  public boolean[] toArray() {
    boolean[] result = new boolean[size];
    System.arraycopy(array, 0, result, 0, size);
    return result;
  }

  // ---------------------------------------------------------------- methods

  /**
   * Returns the element at the specified position in this list.
   */
  public boolean get(int index) {
    checkRange(index);
    return array[index];
  }

  /**
   * Returns the number of elements in this list.
   */
  public int size() {
    return size;
  }

  /**
   * Removes the element at the specified position in this list.
   * Shifts any subsequent elements to the left (subtracts
   * one from their indices).
   *
   @param index the index of the element to remove
   @return the value of the element that was removed
   @throws UnsupportedOperationException when this operation is not
   *                                       supported
   @throws IndexOutOfBoundsException   if the specified index is out of range
   */
  public boolean remove(int index) {
    checkRange(index);
    boolean oldval = array[index];
    int numtomove = size - index - 1;
    if (numtomove > 0) {
      System.arraycopy(array, index + 1, array, index, numtomove);
    }
    size--;
    return oldval;
  }
  /**
   * Removes from this list all of the elements whose index is between fromIndex,
   * inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index).
   */
  public void removeRange(int fromIndex, int toIndex) {
    checkRange(fromIndex);
    checkRange(toIndex);
    if (fromIndex >= toIndex) {
      return;
    }
    int numtomove = size - toIndex;
    if (numtomove > 0) {
      System.arraycopy(array, toIndex, array, fromIndex, numtomove);
    }
    size -= (toIndex - fromIndex);
  }

  /**
   * Replaces the element at the specified position in this list with the specified element.
   *
   @param index   the index of the element to change
   @param element the value to be stored at the specified position
   @return the value previously stored at the specified position
   */
  public boolean set(int index, boolean element) {
    checkRange(index);
    boolean oldval = array[index];
    array[index= element;
    return oldval;
  }

  /**
   * Appends the specified element to the end of this list.
   */
  public void add(boolean element) {
    ensureCapacity(size + 1);
    array[size++= element;
  }

  /**
   * Inserts the specified element at the specified position in this list.
   * Shifts the element currently at that position (if any) and any subsequent
   * elements to the right (adds one to their indices).
   *
   @param index   the index at which to insert the element
   @param element the value to insert
   */
  public void add(int index, boolean element) {
    checkRangeIncludingEndpoint(index);
    ensureCapacity(size + 1);
    int numtomove = size - index;
    System.arraycopy(array, index, array, index + 1, numtomove);
    array[index= element;
    size++;
  }

  /**
   * Appends all of the elements in the specified array to the end of this list.
   */
  public void addAll(boolean[] data) {
    int dataLen = data.length;
    if (dataLen == 0) {
      return;
    }
    int newcap = size + (int) (dataLen * 1.11;
    ensureCapacity(newcap);
    System.arraycopy(data, 0, array, size, dataLen);
    size += dataLen;
  }

  /**
   * Appends all of the elements in the specified array at the specified position in this list.
   */
  public void addAll(int index, boolean[] data) {
    int dataLen = data.length;
    if (dataLen == 0) {
      return;
    }
    int newcap = size + (int) (dataLen * 1.11;
    ensureCapacity(newcap);
    System.arraycopy(array, index, array, index + dataLen, size - index);
    System.arraycopy(data, 0, array, index, dataLen);
    size += dataLen;
  }

  /**
   * Removes all of the elements from this list.
   * The list will be empty after this call returns.
   */
  public void clear() {
    size = 0;
  }

  // ---------------------------------------------------------------- search

  /**
   * Returns true if this list contains the specified element.
   */
  public boolean contains(boolean data) {
    for (int i = 0; i < size; i++) {
      if (array[i== data) {
        return true;
      }
    }
    return false;
  }


  /**
   * Searches for the first occurence of the given argument.
   */
  public int indexOf(boolean data) {
    for (int i = 0; i < size; i++) {
      if (array[i== data) {
        return i;
      }
    }
    return -1;
  }

  /**
   * Returns the index of the last occurrence of the specified object in this list.
   */
  public int lastIndexOf(boolean data) {
    for (int i = size - 1; i >= 0; i--) {
      if (array[i== data) {
        return i;
      }
    }
    return -1;
  }

  /**
   * Tests if this list has no elements.
   */
  public boolean isEmpty() {
    return size == 0;
  }



  // ---------------------------------------------------------------- capacity

  /**
   * Increases the capacity of this ArrayList instance, if necessary,
   * to ensure that it can hold at least the number of elements specified by
   * the minimum capacity argument.
   */
  public void ensureCapacity(int mincap) {
    if (mincap > array.length) {
      int newcap = ((array.length * 3>> 11;
      boolean[] olddata = array;
      array = new boolean[newcap < mincap ? mincap : newcap];
      System.arraycopy(olddata, 0, array, 0, size);
    }
  }

  /**
   * Trims the capacity of this instance to be the list's current size.
   * An application can use this operation to minimize the storage of some instance.
   */
  public void trimToSize() {
    if (size < array.length) {
      boolean[] olddata = array;
      array = new boolean[size];
      System.arraycopy(olddata, 0, array, 0, size);
    }
  }

  // ---------------------------------------------------------------- serializable

  private void writeObject(ObjectOutputStream outthrows IOException {
    out.defaultWriteObject();
    out.writeInt(array.length);
    for (int i = 0; i < size; i++) {
      out.writeBoolean(array[i]);
    }
  }

  private void readObject(ObjectInputStream inthrows IOException, ClassNotFoundException {
    in.defaultReadObject();
    array = new boolean[in.readInt()];
    for (int i = 0; i < size; i++) {
      array[i= in.readBoolean();
    }
  }

  // ---------------------------------------------------------------- privates

  private void checkRange(int index) {
    if (index < || index >= size) {
      throw new IndexOutOfBoundsException("Index should be at least 0 and less than " + size + ", found " + index);
    }
  }

  private void checkRangeIncludingEndpoint(int index) {
    if (index < || index > size) {
      throw new IndexOutOfBoundsException("Index should be at least 0 and at most " + size + ", found " + index);
    }
  }

}

   
    
    
    
    
    
    
    
    
  














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 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
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.