public Element put ( int index , Element newValue ) { Element oldValue = null ; int size = list . size ( ) ; if ( index < 0 ) { if ( index < - size ) { // negative indices do not auto-prepend // (asopposed to positive ones that grow the list by appending undef) throw new EvaluationException ( MessageUtils . format ( MSG_INVALID_NEGATIVE_LIST_INDEX , Integer . toString ( index ) , size ) ) ; } else { index += size ; } } try { if ( ( newValue != null ) && ! ( newValue instanceof Null ) ) { if ( index >= size ) { for ( int i = 0 ; i < index - size ; i ++ ) { list . add ( Undef . VALUE ) ; } list . add ( newValue ) ; } else { oldValue = list . set ( index , newValue ) ; if ( oldValue != null ) { oldValue . checkValidReplacement ( newValue ) ; } } } else { try { oldValue = list . remove ( index ) ; } catch ( IndexOutOfBoundsException ioobe ) { // Ignore this error; removing non-existant element is OK. } } } catch ( IndexOutOfBoundsException ioobe ) { throw new EvaluationException ( MessageUtils . format ( MSG_NONEXISTANT_LIST_ELEMENT , Integer . toString ( index ) ) ) ; } return oldValue ; }
This is an optimized version of the put method which doesn t require creating a Term object .