Java 中 eval() 解析器和二维数组的实现 [英] ImplementIon of eval() parser and 2d array in Java
问题描述
我一直在解决两个与 Java 相关的问题.一个简单,一个更难.
I have really stuck on two Java related issues. One simple and one more difficult.
关于创建一个二维数组,我像这样初始化一个表:
Regarding the creation of a 2D array, I initialize a table like this:
private String [][] table_of_classifiers = null;
然后,在一个函数中,我像这样填充它的内容:
and then, within a function, I fill its contents like this:
String [][] table_of_classifiers = {
{"x1","x","x","x","x"},
{"x2","x","x","x","x"},
{"x3","x","x","x","x"},
{"x4","x","x","x","x"},
{"x5","x","x","x","x"},
{"x6","x","x","x","x"},
};
但是您可以猜到第二个表会覆盖(本地)第一个表,这当然不是我想要做的.我做错了什么?注意表的维度一开始是不知道的.
But as you can guess the second table overwrites (locally) the first one, that is of course not what I want to do. What I do wrong? Note that the dimension of the table is not known from the beginning.
推荐答案
只需:
class Foo {
private String [][] table_of_classifiers = null;
void bar() {
table_of_classifiers = new String[][] {
{"x1","x","x","x","x"},
{"x2","x","x","x","x"},
{"x3","x","x","x","x"},
{"x4","x","x","x","x"},
{"x5","x","x","x","x"},
{"x6","x","x","x","x"},
};
}
}
Java 没有 eval
(因为它是一种编译语言),但它有 反射.不过,这几乎肯定不是您想做的任何事情的最佳方法.
Java doesn't have eval
(because it's a compiled language), but it does have reflection. It's almost certainly not the best approach to whatever it is that you want to do, though.
这篇关于Java 中 eval() 解析器和二维数组的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!