Skip to content

5) Using Arrays

pranithcodes edited this page Apr 6, 2018 · 1 revision

public static void main(String[] args) {

	Object[] data = { "JEXL", 123, 87878l, "UsingArrays", "data" };

	JexlContext context = new MapContext();

	context.set("array", data);
	context.set("out", System.out);

	JexlEngine jexl = new JexlBuilder().create();

	JexlExpression e = jexl.createExpression("out.println(\"3rd Element of array::\t \" + array[3])");
	e.evaluate(context);

}

In the above example, we have defined an array data. Both data array and System.out objects are set into context.

In order to access the elements of an array in the JEXL context, we have to specify the key() defined in the context and the position in the square brackets([ ]). So, array[3] is the correct syntax to access the array “data” but not data[3].

Output: 3rd Element of array:: UsingArrays