我真的停留在两个与Java相关的问题上。一种简单而又一种困难。

关于2D数组的创建,我初始化一个表,如下所示:

private String [][] table_of_classifiers = null;


然后,在一个函数中,我像这样填充其内容:

        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"},
        };


但是,您可以猜测第二张表(本地)覆盖了第一张表,这当然不是我想要做的。我做错了什么?请注意,表格的尺寸从一开始就未知。

最佳答案

做就是了:

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(因为它是一种编译语言),但是确实有reflection。但是,几乎可以肯定,这并不是解决您想要做的任何事情的最佳方法。

10-08 16:32