Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1&1 Internet AG, http://www.1and1.org
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation; either version 2 of the License,
   * or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.sf.beezle.sushi.util;
 
List of int elements. Similar to java.lang.List or java.util.ArrayList, but it stores primitive int values. Generic collections for primitive types whould remove the need for IntArrayList. I implemented only those methods that I acually need.
 
 
 public class IntArrayList implements IntCollectionSerializable {
    
Storage for elements.
 
     private int[] data;

    
Number of data elements actually used.
 
     private int size;
 
     public IntArrayList(int sizeint[] data) {
         this. = size;
         this. = data;
     }
 
     public IntArrayList(int[] init) {
          = init.length;
          = new int[];
         System.arraycopy(init, 0, , 0, );
     }
     //--------------------------------------------------------------
 
    
Creates a new empty List, initial size is 32.
 
     public IntArrayList() {
         this(32);
     }
 
     public IntArrayList(int initialSize) {
          = new int[initialSize];
          = 0;
     }

    
Copy constructor.

Parameters:
orig List that supplies the initial elements for the new List.
 
     public IntArrayList(IntArrayList orig) {
          = new int[orig.data.length];
          = orig.size;
         System.arraycopy(orig.data, 0, , 0, );
     }
 
     //-----------------------------------------------------------------
 
     @Override
     public int hashCode() {
         return size();
     }
     
     @Override
     public boolean equals(Object obj) {
         IntArrayList operand;
         int i;
 
         if (obj instanceof IntArrayList) {
             operand = (IntArrayListobj;
             if ( == operand.size) {
                 for (i = 0; i < i++) {
                     if ([i] != operand.data[i]) {
                         return false;
                     }
                 }
                 return true;
             }
         }
         return false;
     }

    
Gets an element from the List.

Parameters:
idx index of the element asked for
Returns:
selected element
    public int get(int idx) {
        return [idx];
    }

    
Replaces an element in the List.

Parameters:
ele new element
idx index of the element to be replaced
    public void set(int idxint ele) {
        [idx] = ele;
    }
    public void ensureCapacity(int min) {
        int[] tmp;
        int old;
        int capacity;
        old = .;
        if (min > old) {
            tmp = ;
            capacity = (old * 5) / 3 + 1;
            if (capacity < min) {
                capacity = min;
            }
             = new int[capacity];
            System.arraycopy(tmp, 0, , 0, );
        }
    }

    
Adds an element to the List. All following elements are moved up by one index.

Parameters:
idx where to insert the new element
ele new element
    public void add(int idxint ele) {
        ensureCapacity( + 1);
        System.arraycopy(idxidx + 1,  - idx);
        [idx] = ele;
        ++;
    }

    
Adds an element to the end of the List.

Parameters:
ele new element
    public void add(int ele) {
        ensureCapacity( + 1);
        [++] = ele;
    }
    public void addAll(IntArrayList op) {
        ensureCapacity( + op.size);
        System.arraycopy(op.data, 0, op.size);
         += op.size;
    }


    
Removes an element from the List. All following elements are moved down by one index.

Parameters:
idx index of the element to remove
    public void remove(int idx) {
        --;
        System.arraycopy(idx + 1, idx - idx);
    }

    
Removes all elements.
    public void clear() {
         = 0;
    }

    
Searches an element.

Parameters:
ele element to look for
Returns:
index of the first element found; -1 if nothing was found
    public int indexOf(int ele) {
        int i;
        for (i = 0; i < i++) {
            if ([i] == ele) {
                return i;
            }
        }
        return -1;
    }
    public boolean contains(int ele) {
        return indexOf(ele) != -1;
    }

    
Returns the number of elements in the List.

Returns:
number of elements
    public int size() {
        return ;
    }
    public boolean isEmpty() {
        return  == 0;
    }

    
Creates an array with all elements of the List.

Returns:
the array requested
    public int[] toArray() {
        return toArray(new int[]);
    }
    public int[] toArray(int[] result) {
        if (result.length < ) {
            result = new int[];
        }
        System.arraycopy(, 0, result, 0, );
        return result;
    }
    //-----------------------------------------------------------------

    
Returns a string representation.

Returns:
string representation
    @Override
    public String toString() {
        StringBuilder buffer;
        int imax;
        max = size();
        buffer = new StringBuilder();
        for (i = 0; i < maxi++) {
            buffer.append(' ');
            buffer.append(get(i));
        }
        return buffer.toString();
    }
New to GrepCode? Check out our FAQ X