public boolean setElement(int index, E element) {
        if (indexOK(index)) {

            array[index] = element;

            if (index > lastPos && element != null) {
                lastPos = index;
            } else if (index == lastPos && element == null) {
                while (array[lastPos] == null && lastPos > 0) {
                    --lastPos;
                }
            }
            return true;
        }
        return false;
    }