使用MVEL2表达式创建数组并添加字符串元素
可以使用以下代码来使用MVEL2表达式新建一个数组,并且添加一个字符串元素,并打印整个数组:
import org.mvel2.MVEL;
public class Main {
public static void main(String[] args) {
String expression = "new String[]{'element1'}";
String[] array = (String[]) MVEL.eval(expression);
// 添加字符串元素
String newElement = "element2";
String[] newArray = new String[array.length + 1];
System.arraycopy(array, 0, newArray, 0, array.length);
newArray[array.length] = newElement;
// 打印整个数组
for (String element : newArray) {
System.out.println(element);
}
}
}
输出结果为:
element1
element2
原文地址: https://www.cveoy.top/t/topic/fLbS 著作权归作者所有。请勿转载和采集!