ImplementIon of eval() parser and 2d array

2019-07-31 01:23发布

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.

3条回答
We Are One
2楼-- · 2019-07-31 01:41

Just do:

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 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.

查看更多
Viruses.
3楼-- · 2019-07-31 01:49

Regarding your first problem: to assign to table_of_classifiers without redeclaring it, write:

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

Regarding eval . . . the problem is that the run-time doesn't have the names of scoped local variables, and although it can get the names of instance variables, it has to do that within the context of an object. It's possible to address these sorts of issues, but it's non-trivial, and will involve major compromises. I think you have to thoroughly understand how scoping works and how reflection works before you start figuring out what features eval will support, because otherwise you'll just be disappointed at all the requirements you give it that turn out to be impossible.

查看更多
该账号已被封号
4楼-- · 2019-07-31 01:58

Regarding the creation of a 2D array, I initialize a table like this:

private String [][] table_of_classifiers = null;

Not really. This is the declaration and initialization of a variable that can point to a "2d array", "table" or more exact an "array of arrays" of Strings.

Unless you work with that fact that the variable can/will be null, initializing it to null is usually a bad idea, because you need to do extra work to check for null. Examples:

String[][] a;
// ...
String b = a[0][0];

This won't compile, unless a wasn't initialized in the mean time. This is a good thing, because you can avoid a potential bug.

String[][] a = null;
// ...
String b = a[0][0];

This will however will compile, and if you forgot to actually assign the variable a real array, the program will "crash" with a "null pointer exception" or you need to add additional code/work to check for null.

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

You are not "filling" anything here. For something to be filled it must exist first, but you haven't created anything yet.

Here you are declaring a second variable of the same name, which is only possible if you are in a different scope that the first one, and in that case you are "hiding" ("shadowing") the original variable if it originally was accessible from this new scope.

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?

Which "first" table? There was no first table until now, only a first variable. The others have shown you what you need to do to assign the "table" to the original variable, by not using the "declaration" String[][] at the beginning of the line.

Otherwise it's impossible to say what you are "doing wrong" because you haven't really explained what you are attempting to do.

Note that the dimension of the table is not known from the beginning.

It's not? How/why are you using a array literal then? Literal arrays are for creating arrays of a fixed size with a fixed "prefilling".

What exactly do mean with "the beginning"? Isn't the size known when you are programming (during compile time) or when the program starts (at run time)?

If you get the size of the array during run time you can create a normal array with new:

int a = ...; 
int b = ...; // Get the sizes from somewhere, e.g, user input 

String[][] table_of_classifiers = new String[a][b];
// Now you have an "empty" table

If size "changes" during run time, then - depending on what you are actually attempting to do - then an array is the wrong tool and you should be using a List implementation such as ArrayList instead.


Regarding "eval", as the others say, Java is a compiled language making "eval" basically impossible. The is "reflection" or the use of Class types to achieve what you are hinting at, but you really need to explain much more extensively what you are trying to achieve, then it may be possible to help you here.

However reflection and CLass types are a complicated matter, and considering you are obviously struggling with the most basic Java concepts, you have a long way to go to until you will be able to do what you want to do.

查看更多
登录 后发表回答