Exemplo n.º 1
0
 /**
  * Adds an element to the end of this expandable array.
  *
  * @param value to be added to end of array
  */
 public synchronized void addElement(double value) {
   numElements++;
   if ((startIndex + numElements) > internalArray.length) {
     expand();
   }
   internalArray[startIndex + (numElements - 1)] = value;
   if (shouldContract()) {
     contract();
   }
 }
Exemplo n.º 2
0
  /**
   * Adds an element to the end of the array and removes the first element in the array. Returns the
   * discarded first element. The effect is similar to a push operation in a FIFO queue.
   *
   * <p>Example: If the array contains the elements 1, 2, 3, 4 (in that order) and
   * addElementRolling(5) is invoked, the result is an array containing the entries 2, 3, 4, 5 and
   * the value returned is 1.
   *
   * @param value the value to be added to the array
   * @return the value which has been discarded or "pushed" out of the array by this rolling insert
   */
  public synchronized double addElementRolling(double value) {
    double discarded = internalArray[startIndex];

    if ((startIndex + (numElements + 1)) > internalArray.length) {
      expand();
    }
    // Increment the start index
    startIndex += 1;

    // Add the new value
    internalArray[startIndex + (numElements - 1)] = value;

    // Check the contraction criteria
    if (shouldContract()) {
      contract();
    }
    return discarded;
  }