Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

ArrayList.java

00001 package ca.mcgill.sable.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * SableUtil, a clean room implementation of the Collection API.     *
00005  * Copyright (C) 1997, 1998 Raja Vallee-Rai (kor@sable.mcgill.ca).   *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * Modifications by Etienne Gagnon (gagnon@sable.mcgill.ca) are      *
00009  * Copyright (C) 1998 Etienne Gagnon (gagnon@sable.mcgill.ca).  All  *
00010  * rights reserved.                                                  *
00011  *                                                                   *
00012  * Modifications by Shawn Laubach (laubach@acm.org) are              *
00013  * Copyright (C) 1998 Shawn Laubach (laubach@acm.org).  All rights   *
00014  * reserved.                                                         *
00015  *                                                                   *
00016  * This work was done as a project of the Sable Research Group,      *
00017  * School of Computer Science, McGill University, Canada             *
00018  * (http://www.sable.mcgill.ca/).  It is understood that any         *
00019  * modification not identified as such is not covered by the         *
00020  * preceding statement.                                              *
00021  *                                                                   *
00022  * This work is free software; you can redistribute it and/or        *
00023  * modify it under the terms of the GNU Library General Public       *
00024  * License as published by the Free Software Foundation; either      *
00025  * version 2 of the License, or (at your option) any later version.  *
00026  *                                                                   *
00027  * This work is distributed in the hope that it will be useful,      *
00028  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00029  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00030  * Library General Public License for more details.                  *
00031  *                                                                   *
00032  * You should have received a copy of the GNU Library General Public *
00033  * License along with this library; if not, write to the             *
00034  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00035  * Boston, MA  02111-1307, USA.                                      *
00036  *                                                                   *
00037  * To submit a bug report, send a comment, or get the latest news on *
00038  * this project and other Sable Research Group projects, please      *
00039  * visit the web site: http://www.sable.mcgill.ca/                   *
00040  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00041 
00042 /*
00043  Reference Version
00044  -----------------
00045  This is the latest official version on which this file is based.
00046  The reference version is: $SableUtilVersion: 1.11 $
00047 
00048  Change History
00049  --------------
00050  A) Notes:
00051 
00052  Please use the following template.  Most recent changes should
00053  appear at the top of the list.
00054 
00055  - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate]
00056    [description of modification].
00057 
00058  Any Modification flagged with "(*)" was done as a project of the
00059  Sable Research Group, School of Computer Science,
00060  McGill University, Canada (http://www.sable.mcgill.ca/).
00061 
00062  You should add your copyright, using the following template, at
00063  the top of this file, along with other copyrights.
00064 
00065  *                                                                   *
00066  * Modifications by [name] are                                       *
00067  * Copyright (C) [year(s)] [your name (or company)].  All rights     *
00068  * reserved.                                                         *
00069  *                                                                   *
00070 
00071  B) Changes:
00072 
00073  - Modified on November 19, 1998 by Shawn Laubach (laubach@acm.org).
00074    Added set(int, Object).
00075 
00076  - Modified on September 25, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00077    Changed a call to removeElementAt() to remove().
00078 
00079  - Modified on July 23, 1998 by Etienne Gagnon (gagnon@sable.mcgill.ca). (*)
00080    Added toArray(Object[]).
00081 
00082  - Modified on June 15, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00083    First release of this file.
00084 */
00085 
00086 public class ArrayList extends AbstractList
00087 {
00088     private static final int DEFAULT_SIZE = 8;
00089 
00090     private int numElements;
00091     private int maxElements;
00092     private Object[] elements;
00093 
00094     private class ArrayIterator implements Iterator
00095     {
00096         int nextIndex;
00097 
00098         ArrayIterator()
00099         {
00100             nextIndex = 0;
00101         }
00102 
00103         public boolean hasNext()
00104         {
00105             return nextIndex < numElements;
00106         }
00107 
00108         public Object next() throws NoSuchElementException
00109         {
00110             if(!(nextIndex < numElements))
00111                 throw new NoSuchElementException();
00112 
00113             return elements[nextIndex++];
00114         }
00115 
00116         public void remove() throws NoSuchElementException
00117         {
00118             if(nextIndex == 0)
00119                 throw new NoSuchElementException();
00120             else
00121             {
00122                 ArrayList.this.remove(nextIndex - 1);
00123                 nextIndex = nextIndex - 1;
00124             }
00125         }
00126     }
00127 
00128     public ArrayList()
00129     {
00130         maxElements = DEFAULT_SIZE;
00131         elements = new Object[DEFAULT_SIZE];
00132         numElements = 0;
00133     }
00134     public void add(int index, Object e)
00135     {
00136         // Expaxpand array if necessary
00137             if(numElements == maxElements)
00138                 doubleCapacity();
00139 
00140         // Handle simple case
00141             if(index == numElements)
00142             {
00143                 elements[numElements++] = e;
00144                 return;
00145             }
00146 
00147         // Shift things over
00148             System.arraycopy(elements, index, elements, index + 1, numElements - index);
00149             elements[index] = e;
00150             numElements++;
00151     }
00152     public boolean add(Object e)
00153     {
00154         // Expand array if necessary
00155             if(numElements == maxElements)
00156                 doubleCapacity();
00157 
00158         // Add element
00159             elements[numElements++] = e;
00160             return true;
00161     }
00162     public void clear()
00163     {
00164         numElements = 0;
00165     }
00166     public boolean contains(Object obj)
00167     {
00168         for(int i = 0; i < numElements; i++)
00169             if(elements[i].equals(obj))
00170                 return true;
00171 
00172         return false;
00173     }
00174     private void doubleCapacity()
00175     {
00176         int newSize = maxElements * 2;
00177 
00178         Object[] newElements = new Object[newSize];
00179 
00180         System.arraycopy(elements, 0, newElements, 0, numElements);
00181         elements = newElements;
00182         maxElements = newSize;
00183     }
00184     public Object get(int index)
00185     {
00186         return elements[index];
00187     }
00188     public Iterator iterator()
00189     {
00190         return new ArrayIterator();
00191     }
00192     public Object remove(int index)
00193     {
00194         Object toReturn = get(index);
00195         removeElementAt(index);
00196 
00197         return toReturn;
00198     }
00199     private void removeElementAt(int index)
00200     {
00201         // Handle simple case
00202             if(index  == numElements - 1)
00203             {
00204                 numElements--;
00205                 return;
00206             }
00207 
00208         // Else, shift over elements
00209             System.arraycopy(elements, index + 1, elements, index, numElements - (index + 1));
00210             numElements--;
00211     }
00212     public Object set(int index, Object e)
00213     {
00214       elements[index] = e;
00215       return null;
00216     }
00217     public int size()
00218     {
00219         return numElements;
00220     }
00221     public Object[] toArray()
00222     {
00223         Object[] array = new Object[numElements];
00224 
00225         System.arraycopy(elements, 0, array, 0, numElements);
00226         return array;
00227     }
00228     public void toArray(Object[] array)
00229     {
00230         System.arraycopy(elements, 0, array, 0, numElements);
00231     }
00232 }

Generated at Thu Feb 7 06:39:31 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001