Reputation: 978
Using JEXL, I am trying to initialize array and than adding elements into it, however below code gives me 'unsolvable property '0' error.
var abc=[];
abc[0]=5;
1) How can I initialize empty array and keep adding values in it?
2) Can I use it like List, where I do not need to specify size at the time of initialization ?
Upvotes: 0
Views: 4212
Reputation: 266
In JEXL, the syntax []
creates a Java array, not a List
. As an array, it has a fixed size, so you cannot add values to it. However, JEXL 3.2 has a new syntax for creating an ArrayList literal. Basically, you add ...
as the final element.
So in JEXL 3.2, your example could be written as:
var abc=[...];
abc.add(5);
See the JEXL literal syntax reference for more information.
Upvotes: 0
Reputation: 58774
in JEXL syntax you can initialize objects with new
function.
Other option is to add to context arraylist:
This is a working example with jexl2:
JexlEngine jexl = new JexlEngine();
String jexlExp = "var abc=new(\"java.util.ArrayList\", 1);abc[0]=5";
Expression e = jexl.createExpression( jexlExp );
List<Integer> abc = new ArrayList<>(1);
JexlContext jc = new MapContext();
//jc.set("abc", abc ); second option to add arraylist to context
Object o = e.evaluate(jc);
Upvotes: 0