1

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.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
user706838
  • 5,132
  • 14
  • 54
  • 78
  • See [Is there an eval() function in Java?](http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) – Raedwald Dec 10 '14 at 13:04
  • OP moved second question to a [new post](http://stackoverflow.com/questions/8666935/implemention-of-eval-in-java) – Raedwald Dec 10 '14 at 13:07

3 Answers3

1

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.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • Thank you RoToRa. However, there was no need of giving so much details! The answer was much more easy that you first thought! – user706838 Dec 19 '11 at 15:08
0

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

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.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Thank you ruakh! However, I was looking for a two-step implementation. Could you please help me on how to create the parser? It is very important for me because I will then be able to make more a compact and maintainable code! – user706838 Dec 19 '11 at 15:22
  • @eualin: I promise you that creating your own `eval`, writing your own parser from scratch, will *not* make for more compact and maintainable code. The rest of your code may be more compact, but the parser itself will be huge, complicated, and buggy. – ruakh Dec 19 '11 at 15:34
  • Thanks @ruakh. So, what you would recommend me? Any specific example/approach? I don't think we can't find a solution to that problem... I am not saying it is trivial, I am just saying that it shouldn't be so difficult at all, at least, given the aforementioned requirements... – user706838 Dec 19 '11 at 17:07
  • @eualin: I'm a bit confused. You've already accepted Oli Charlesworth's answer. Are you not satisfied with it? – ruakh Dec 19 '11 at 17:44
  • Had you read that comment more carefully, you could understand that I am still trying to figure out how to implement a hand-made eval() function for Java. Any ideas? – user706838 Dec 19 '11 at 19:42
  • @eualin: I did read your comment. But I don't understand *why* you're still trying to implement such a function, after you accepted an answer that essentially told you not to. – ruakh Dec 19 '11 at 19:46
  • Well, once again: I opened that thread for asking TWO questions! The first was SOLVED! The second is PENDING! Any ideas! – user706838 Dec 19 '11 at 20:19
  • @eualin: When you accept an answer, you're indicating that your question is answered, your problem is solved. If you still have a question that's PENDING, then you should ask it as a new question. – ruakh Dec 19 '11 at 20:30
  • Well. Then, where I am supposed to re-ask this question? Should I re-open a new thread? – user706838 Dec 19 '11 at 22:36
  • @eualin: Yes, please do. (And once you've done so, please edit this question to add a link to that one, so that people coming across this one in search of Java `eval` can find the follow-up.) – ruakh Dec 19 '11 at 22:41
  • Please, take a look here [link](http://stackoverflow.com/questions/8666935/implemention-of-eval-in-java) . – user706838 Dec 29 '11 at 10:59