Advertisement
Guest User

Generic List

a guest
Jun 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class GenericList
  2. {
  3.     // class constant for default size
  4.     private static final int DEFAULT_CAP = 10;
  5.    
  6.     //instance variables
  7.     // iValues store the elements of the list and
  8.     // may have extra capacity
  9.     private Object[] iValues;
  10.     private int iSize;
  11.    
  12.     /**
  13.      * Default add method. Add x to the end of this IntList.
  14.      * Size of the list goes up by 1.
  15.      * @param x The value to add to the end of this list.
  16.      */
  17.     public void add(Object x){
  18.         insert(iSize, x);
  19.     }
  20.    
  21.     public Object get(int pos){
  22.         return iValues[pos];
  23.     }
  24.    
  25.     /**
  26.      * Insert obj at position pos.
  27.      * post: get(pos) = x, size() = old size() + 1
  28.      * @param pos 0 <= pos <= size()
  29.      * @param obj The element to add.
  30.      */
  31.     public void insert(int pos, Object obj){
  32.         ensureCapcity();
  33.         for(int i = iSize; i > pos; i--){
  34.             iValues[i] = iValues[i - 1];
  35.         }
  36.         iValues[pos] = obj;
  37.         iSize++;
  38.     }
  39.    
  40.     public Object remove(int pos){
  41.         Object removedValue = iValues[pos];
  42.         for(int i = pos; i < iSize - 1; i++)
  43.             iValues[i] = iValues[i + 1];
  44.         iValues[iSize - 1] = null;
  45.         iSize--;
  46.         return removedValue;
  47.     }
  48.    
  49.     private void ensureCapcity(){
  50.         // is there extra capacity available?
  51.         // if not, resize
  52.         if(iSize == iValues.length)
  53.             resize();
  54.     }
  55.    
  56.     public int size(){
  57.         return iSize;
  58.     }
  59.    
  60.     // resize internal storage container by a factor of 2
  61.     private void resize() {
  62.         Object[] temp = new Object[iValues.length * 2];
  63.         System.arraycopy(iValues, 0, temp, 0, iValues.length);
  64.         iValues = temp;
  65.     }
  66.    
  67.     /**
  68.      * Return a String version of this list. Size and
  69.      * elements included.
  70.      */
  71.     public String toString(){
  72.         // we could make this more effecient by using a StringBuffer.
  73.         // See alternative version
  74.         String result = "size: " + iSize + ", elements: [";
  75.         for(int i = 0; i < iSize - 1; i++)
  76.             result += iValues[i].toString() + ", ";
  77.         if(iSize > 0 )
  78.             result += iValues[iSize - 1];
  79.         result += "]";
  80.         return result;
  81.     }
  82.    
  83.     // Would not really have this and toString available
  84.     // both included just for testing
  85.     public String toStringUsingStringBuffer(){
  86.         StringBuffer result = new StringBuffer();
  87.         result.append( "size: " );
  88.         result.append( iSize );
  89.         result.append(", elements: [");
  90.         for(int i = 0; i < iSize - 1; i++){
  91.             result.append(iValues[i]);
  92.             result.append(", ");
  93.         }
  94.         if( iSize > 0 )
  95.             result.append(iValues[iSize - 1]);
  96.         result.append("]");
  97.         return result.toString();
  98.     }
  99.  
  100.     /**
  101.      * Default constructor. Creates an empty list.
  102.      */
  103.     public GenericList(){
  104.         //redirect to single int constructor
  105.         this(DEFAULT_CAP);
  106.         //other statments could go here.
  107.     }
  108.  
  109.     /**
  110.      * Constructor to allow user of class to specify
  111.      * initial capacity in case they intend to add a lot
  112.      * of elements to new list. Creates an empty list.
  113.      * @param initialCap > 0
  114.      */    
  115.     public GenericList(int initialCap) {
  116.         assert initialCap > 0 : "Violation of precondition. IntListVer1(int initialCap):"
  117.             + "initialCap must be greater than 0. Value of initialCap: " + initialCap;
  118.         iValues = new Object[initialCap];
  119.         iSize = 0;
  120.     }
  121.    
  122.    /**
  123.     * Return true if this IntList is equal to other.<br>
  124.     * pre: none
  125.     * @param other The object to comapre to this
  126.     * @return true if other is a non null, IntList object
  127.     * that is the same size as this IntList and has the
  128.     * same elements in the same order, false otherwise.
  129.     */
  130.     public boolean equals(Object other){
  131.         boolean result;
  132.         if(other == null)
  133.             // we know this is not null so can't be equal
  134.             result = false;
  135.         else if(this == other)
  136.             // quick check if this and other refer to same IntList object
  137.             result = true;
  138.         else if( this.getClass() != other.getClass() )
  139.             // other is not an IntList they can't be equal
  140.             result = false;
  141.         else{
  142.             // other is not null and refers to an IntList
  143.             GenericList otherList = (GenericList)other;
  144.             result = this.size() == otherList.size();
  145.             int i = 0;
  146.             while(i < iSize && result){
  147.                 result = this.iValues[i].equals( otherList.iValues[i] );
  148.                 i++;
  149.             }
  150.         }
  151.         return result;    
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement