Page 1 of 1

replaceElement / putElement / setElement

Posted: 05 Nov 2014 10:45
by Bushmills
using addElement(list,index,object) for replacing a list element, with index coming from a variable, results in this code:

Code: Select all

if (index < length(list)) removeElement(list,index);
addElement(list,index,object);
Just using addElement(list,index,object) inserts object at index, growing the list, while removeElement when index is at the end of list, errors out. The above test is ok for occasional list item replacements, but with scripts doing many of those this quickly becomes bothersome.

The alternative is to populate the list with the number of items it will be expected to contain at its maximum. That way, there will always be a replaceable element, and the code shrinks to

Code: Select all

removeElement(list,index);
addElement(list,index,object);
with the drawback that lists may carry many possibly unneeded list elements, solely present to warrant proper function of the non-conditional removeElement()

I'd like to be able to simply putElement(list,index,object) without bothering about insertion or insert position. To demonstrate the need, I am prepared to send you a flow, or a screenshot of one of its scripts . The script action "System" should be most illustrative:

http://automagic4android.com/flow.php?i ... 3ca59cc11e