proj/proj1b/ArrayDeque.java
private void resize() {

        // condition 1 and 2
        if (checkUsageR() || (size == Array.length)) {

            int targetSize = Array.length;

            if (checkUsageR()) {
                targetSize /= 2;
            } else {
                targetSize *= 2;
            }

            // New Array
            Item[] NewArray = (Item[]) new Object[targetSize];

            // Copy from the first to the last element of the original array.
            // Start index setting
            int maxIndex = 0;
            int startIndex = 0;

            if (checkUsageR()) {
                // maxIndex = Math.max(nextFirst, nextLast) + 1;

                // Deal with startIndex when usage is pretty low -> Dummy approach
                for (int i = 0; i < Array.length - 1; i++) {
                    if (Array[i] == null && Array[i + 1] != null) {
                        startIndex = i + 1;
                    }
                    if (Array[i] != null && Array[i + 1] == null) {
                        maxIndex = i;
                    }
                }

                System.arraycopy(Array, startIndex, NewArray, 1, maxIndex - startIndex + 1);

                Array = NewArray;

                // pointers setting
                nextFirst = 0;
                nextLast = maxIndex - startIndex + 2;

            } else {
                maxIndex = Array.length;
                startIndex = 0;

                System.arraycopy(Array, startIndex, NewArray, 1, maxIndex - startIndex);

                Array = NewArray;

                // pointers setting
                nextFirst = 0;
                nextLast = maxIndex - startIndex + 1;
            }

        }

    }
/**  resize an array.
     *
     *   1. For arrays of length 16 or more, usage factor should always be at least 25%
     *      Otherwise halve the array.
     *      For smaller arrays, usage factor can be arbitrarily low.
     *
     *      Usage factor < 25% < 50%. So we can shrink the array size by 2 with no overflow.
     *
     *      References:
     *          Gitbook - Chapter 2.5: Memory Performance
     *
     *
     *   2. Array is full and enlarge the Array.
     *
     *      Enlarge Factor = 2
     *
     *
     *   3. My strategy for adjusting capacity of Array:
     *
     *      1) shrinking size
     *
     *      2) increasing size
     *
     *      [current array]: (1 2 3 4 5 6 7 8)
     *      whenever we want to add a new element, we need increase the capacity of
     *      current array, the strategy I applied is as follows
     *
     *      (1 2 3 4 5 6 7 8) -> (null 1 2 3 4 5 6 7 8 null ...)
     *
     *      that means I use `System.arraycopy` function copy all the elements in oringinal
     *      array to a new array with default starting index `1`. And I move the First pointer
     *      to the first position of the new Array (nextFirst = 0;) and Last pointer to the last
     *      element position of the new Array.
     *
     *      Though, it seems a little dummy... :)
     *
     * */
resize an array.
1. For arrays of length 16 or more, usage factor should always be at least 25%
Otherwise halve the array.
For smaller arrays, usage factor can be arbitrarily low.





2. Array is full and enlarge the Array.

Enlarge Factor = 2

3. My strategy for adjusting capacity of Array.

1) shrinking size

2) increasing size

[current array]: (1 2 3 4 5 6 7 8)
whenever we want to add a new element, we need increase the capacity of
current array, the strategy I applied is as follows



that means I use `System.arraycopy` function copy all the elements in oringinal
array to a new array with default starting index `1`. And I move the First pointer
to the first position of the new Array (nextFirst = 0;) and Last pointer to the last
element position of the new Array.

Though, it seems a little dummy...
resize an array . 1 . For arrays of length 16 or more usage factor should always be at least 25% Otherwise halve the array . For smaller arrays usage factor can be arbitrarily low . 2 . Array is full and enlarge the Array . Enlarge Factor = 2 3 . My strategy for adjusting capacity of Array . 1 ) shrinking size 2 ) increasing size [ current array ] : ( 1 2 3 4 5 6 7 8 ) whenever we want to add a new element we need increase the capacity of current array the strategy I applied is as follows that means I use ` System . arraycopy ` function copy all the elements in oringinal array to a new array with default starting index ` 1 ` . And I move the First pointer to the first position of the new Array ( nextFirst = 0 ; ) and Last pointer to the last element position of the new Array . Though it seems a little dummy ...
private void resize() {
        if (checkUsageR() || (size == Array.length)) {
            int targetSize = Array.length;
            if (checkUsageR()) {
                targetSize /= 2;
            } else {
                targetSize *= 2;
            }
            Item[] NewArray = (Item[]) new Object[targetSize];
            int maxIndex = 0;
            int startIndex = 0;
            if (checkUsageR()) {
                for (int i = 0; i < Array.length - 1; i++) {
                    if (Array[i] == null && Array[i + 1] != null) {
                        startIndex = i + 1;
                    }
                    if (Array[i] != null && Array[i + 1] == null) {
                        maxIndex = i;
                    }
                }
                System.arraycopy(Array, startIndex, NewArray, 1, maxIndex - startIndex + 1);
                Array = NewArray;
                nextFirst = 0;
                nextLast = maxIndex - startIndex + 2;
            } else {
                maxIndex = Array.length;
                startIndex = 0;
                System.arraycopy(Array, startIndex, NewArray, 1, maxIndex - startIndex);
                Array = NewArray;
                nextFirst = 0;
                nextLast = maxIndex - startIndex + 1;
            }
        }
    }
private void resize ( ) { if ( checkUsageR ( ) || ( size == Array . length ) ) { int targetSize = Array . length ; if ( checkUsageR ( ) ) { targetSize /= 2 ; } else { targetSize *= 2 ; } Item [ ] NewArray = ( Item [ ] ) new Object [ targetSize ] ; int maxIndex = 0 ; int startIndex = 0 ; if ( checkUsageR ( ) ) { for ( int i = 0 ; i < Array . length - 1 ; i ++ ) { if ( Array [ i ] == null && Array [ i + 1 ] != null ) { startIndex = i + 1 ; } if ( Array [ i ] != null && Array [ i + 1 ] == null ) { maxIndex = i ; } } System . arraycopy ( Array , startIndex , NewArray , 1 , maxIndex - startIndex + 1 ) ; Array = NewArray ; nextFirst = 0 ; nextLast = maxIndex - startIndex + 2 ; } else { maxIndex = Array . length ; startIndex = 0 ; System . arraycopy ( Array , startIndex , NewArray , 1 , maxIndex - startIndex ) ; Array = NewArray ; nextFirst = 0 ; nextLast = maxIndex - startIndex + 1 ; } } }
resize an array.
resize an array .
// condition 1 and 2 // New Array // Copy from the first to the last element of the original array. // Start index setting // maxIndex = Math.max(nextFirst, nextLast) + 1; // Deal with startIndex when usage is pretty low -> Dummy approach // pointers setting // pointers setting