A growable array of int values, suitable for use with multiple threads : Custom List « 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 » Custom List 




A growable array of int values, suitable for use with multiple threads
 

/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */

/**
 * A growable array of int values, suitable for use with multiple threads.
 */
public class ThreadSafeIntList {
  protected int[] data; // This array holds the integers

  protected int size; // This is how many it current holds

  // Static final values are constants. This one is private.
  private static final int DEFAULT_CAPACITY = 8;

  // Create a ThreadSafeIntList with a default capacity
  public ThreadSafeIntList() {
    // We don't have to set size to zero because newly created objects
    // automatically have their fields set to zero, false, and null.
    data = new int[DEFAULT_CAPACITY]// Allocate the array
  }

  // This constructor returns a copy of an existing ThreadSafeIntList.
  // Note that it synchronizes its access to the original list.
  public ThreadSafeIntList(ThreadSafeIntList original) {
    synchronized (original) {
      this.data = (int[]) original.data.clone();
      this.size = original.size;
    }
  }

  // Return the number of ints stored in the list
  public synchronized int size() {
    return size;
  }

  // Return the int stored at the specified index
  public synchronized int get(int index) {
    if (index < || index >= size// Check that argument is legitimate
      throw new IndexOutOfBoundsException(String.valueOf(index));
    return data[index];
  }

  // Append a new value to the list, reallocating if necessary
  public synchronized void add(int value) {
    if (size == data.length)
      setCapacity(size * 2)// realloc if necessary
    data[size++= value; // add value to list
  }

  // Remove all elements from the list
  public synchronized void clear() {
    size = 0;
  }

  // Copy the contents of the list into a new array and return that array
  public synchronized int[] toArray() {
    int[] copy = new int[size];
    System.arraycopy(data, 0, copy, 0, size);
    return copy;
  }

  // Reallocate the data array to enlarge or shrink it.
  // Not synchronized because it is always called from synchronized methods.
  protected void setCapacity(int n) {
    if (n == data.length)
      return// Check size
    int[] newdata = new int[n]// Allocate the new array
    System.arraycopy(data, 0, newdata, 0, size)// Copy data into it
    data = newdata; // Replace old array
  }
}

   
  














Related examples in the same category
1.Redundancy Checker
2.Doubly-linked list with data structureDoubly-linked list with data structure
3.Sorted listSorted list
4.List with first and last referencesList with first and last references
5.Frist last list
6.Another Link list
7.Implements a "rolling 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.