在if语句中创建对象并在以后使用它 [英] Create Object in if-statement and use it later

查看:203
本文介绍了在if语句中创建对象并在以后使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Infix表示法写一个Parser。
在if语句中,我声明变量newchild。否则我希望它抛出异常。但是当我超出范围时,编译器不再知道变量。
我不能在if语句之前声明它,因为根据我们所处的情况,变量被分配了不同的数据类型。

I am writing a Parser for Infix Notation. In the if-statement I declare the variable newchild. Otherwise I want it to throw an exception. But when I am out of the scope the Compiler does not know the variable anymore. I cannot declare it before the if-statement, because the variable gets assigned a different data type depending on the case we are in.

我能做什么解决这个问题?

What can I do to fix this?

public class ParserForInfixNotation {

public Node parse(List<String> tokenList) {

    Stack<String> myStack = new Stack<String>();
    int i =1;
    while(i <= tokenList.size()){                           //wir gehen alle Eintraege in der Liste durch
        if(Character.isDigit(tokenList.get(i).charAt(1))){
            int value = Integer.parseInt(tokenList.get(i));     //falls der Eintrag eine Zahl ist, wird ein neuer Leaf erstellt
            Leaf res = new Leaf(value);
        }
        else if(tokenList.get(i) == "("){                       // falls der Eintrag eine Klammer ist, wird geschaut, ob in der Klammer ein Unary oder Binary OpNode definiert ist
            if (tokenList.get(i+1) == "-") {
                                                                // Fall Unary
                int j = i+1;
                                                                //am Liebsten ein rekursiver Aufruf auf parser mit nem TeilString des Oberen Strings, der genau den naechsten Node beschreibt
                int anzahlklammern = 0;
                boolean end = false;
                if((Character.isDigit(tokenList.get(j).charAt(1))) || (tokenList.get(j+1) == ")")){
                    Leaf newchild = new Leaf(Integer.parseInt(tokenList.get(j)));
                }
                else if(tokenList.get(j) == "("){
                    while(!end){
                        if(tokenList.get(j) == ")" && anzahlklammern == 1){
                            end = true;
                        }
                        else if(tokenList.get(j) == ")" && j > i+3){                    //die Klammer muss mindestens 2 Stellen enthalten
                            anzahlklammern--;
                            j++;
                        }
                        else if(tokenList.get(j) == "("){
                            anzahlklammern++;
                            j++;
                        }
                        else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == "+" || tokenList.get(j) == "*" || tokenList.get(j) == "-"){
                            j++;
                        }
                        else{
                            throw new IllegalArgumentException();
                        }
                    }
                    List<String> neu = new ArrayList<>();

                    for (int l = i+2; l<j;l++){
                        neu.add(tokenList.get(l));
                    }
                    Node newchild = parse(neu);
                }
                else {
                    throw new IllegalArgumentException();
                }

                UnaryOpNode res = new UnaryOpNode('-',newchild);
            }
            else if((tokenList.get(i+1) == "(") || (Character.isDigit(tokenList.get(i+1).charAt(1)))){ //Fall Binary
                if (Character.isDigit(tokenList.get(i+1).charAt(1)) && (tokenList.get(i+2) == "+" || tokenList.get(i+2) == "*")){
                    Leaf newchildleft = new Leaf(Integer.parseInt(tokenList.get(i+1)));
                    if(tokenList.get(i+2) == "+"){
                        Character operator = '+';
                    }
                    else if(tokenList.get(i+2) == "*"){
                        Character operator = '*';
                    }
                    int j = i+3;
                    if(Character.isDigit(tokenList.get(j).charAt(1))){
                        Leaf newchildright = new Leaf(Integer.parseInt(tokenList.get(j)));
                    }
                    else if(tokenList.get(j) == "("){
                        boolean end = false;
                        int anzahlklammern =0 ;
                        while(!end){
                            if(tokenList.get(j) == ")" && anzahlklammern == 1){
                                end = true;
                            }
                            else if(tokenList.get(j) == ")" && j > i+5){                    //die Klammer muss mindestens 2 Stellen enthalten
                                anzahlklammern--;
                                j++;
                            }
                            else if(tokenList.get(j) == "("){
                                anzahlklammern++;
                                j++;
                            }
                            else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == "+" || tokenList.get(j) == "*" || tokenList.get(j) == "-"){
                                j++;
                            }
                            else{
                                throw new IllegalArgumentException();
                            }
                        }
                        List<String> neu = new ArrayList<>();

                        for (int l = i+4; l<j;l++){
                            neu.add(tokenList.get(l));
                        }
                        Node newrightchild = parse(neu);
                    }
                    else{
                        throw new IllegalArgumentException();
                    }
                }
                else if(tokenList.get(i+1) == "("){
                    int j= i+1;
                    boolean end = false;
                    int anzahlklammern =0 ;
                    while(!end){
                        if(tokenList.get(j) == ")" && anzahlklammern == 1){
                            end = true;
                        }
                        else if(tokenList.get(j) == ")" && j > i+3){                    //die Klammer muss mindestens 2 Stellen enthalten
                            anzahlklammern--;
                            j++;
                        }
                        else if(tokenList.get(j) == "("){
                            anzahlklammern++;
                            j++;
                        }
                        else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == "+" || tokenList.get(j) == "*" || tokenList.get(j) == "-"){
                            j++;
                        }
                        else{
                            throw new IllegalArgumentException();
                        }
                    }
                    List<String> neu = new ArrayList<>();

                    for (int l = i+2; l<j;l++){
                        neu.add(tokenList.get(l));
                    }
                    Node newleftchild = parse(neu);
                    if(tokenList.get(j+1) == "+"){
                        Character operator = '+';
                    }
                    else if(tokenList.get(j+1) == "*"){
                        Character operator = '*';
                    }
                    else{
                        throw new IllegalArgumentException();
                    }
                    if(tokenList.get(j+2)== "("){
                        int k= j+3;
                        end = false;
                        anzahlklammern =0 ;
                        while(!end){
                            if(tokenList.get(k) == ")" && anzahlklammern == 1){
                                end = true;
                            }
                            else if(tokenList.get(k) == ")" && k > j+5){                    //die Klammer muss mindestens 2 Stellen enthalten
                                anzahlklammern--;
                                k++;
                            }
                            else if(tokenList.get(k) == "("){
                                anzahlklammern++;
                                k++;
                            }
                            else if ((Character.isDigit(tokenList.get(k).charAt(1))) || tokenList.get(k) == "+" || tokenList.get(k) == "*" || tokenList.get(k) == "-"){
                                k++;
                            }
                            else{
                                throw new IllegalArgumentException();
                            }
                        }
                        List<String> neu2 = new ArrayList<>();

                        for (int l = j+4; l<k;l++){
                            neu.add(tokenList.get(l));
                        }
                        Node newrightchild = parse(neu2);
                    }
                    else if(Character.isDigit(tokenList.get(j+2).charAt(1))){
                        Leaf newrightchild = new Leaf(Integer.parseInt(tokenList.get(j+2)));
                    }
                    else{
                        throw new IllegalArgumentException();
                    }
                }
                BinaryOpNode res = new BinaryOpNode(operator, newleftchild, newrightchild);
            }
            else{
                throw new IllegalArgumentException();
            }
        }
        else{
            throw new IllegalArgumentException();
        }
    }



    return res; 
}


推荐答案

这是关于范围。变量的范围是声明的块,并在其中阻塞。无论块是 if 语句的块,还是其他语句,或者只是为了定义范围而放在那里的块都无关紧要。

This is about scope. A variable's scope is the block in which it is declared, and blocks within that. It doesn't matter whether the block is the block of an if statement, some other statement, or just a block put there for the purpose of defining scope.

您遇到此问题:

{
    Leaf leaf = new Leaf();
}

doSomethingWith(leaf); // compiler error - there is no `leaf` in this scope.

您可以通过以下方式修复:

You can fix it with:

 Leaf leaf;
 {
      leaf = new Leaf();
 }
 doSomethingWith(leaf);

如果有可能转让给 leaf 不会发生 - 例如,如果它在 if 块中,那么你会得到编译器错误,说变量leaf可能没有已初始化。您可以通过首先初始化为某些回退值来解决此问题。它通常为空:

If there is a possibility that the assignment to leaf won't happen -- for example if it's in an if block, then you'll get a compiler error saying variable leaf may not have been initialized. You can fix this by initialising to some fallback value first. It's often null:

 Leaf leaf = null;
 if(...) {
      leaf = new Leaf();
 }
 doSomethingWith(leaf);

但现在你的代码必须应对 leaf == null的可能性 - 这导致代码过于复杂或脆弱。找到避免分配null的方法,或者如果不能,则保持易受攻击的代码块被隔离,以便该范围之外的任何内容都不需要处理null变量的可能性。

But now your code has to cope with the possibility that leaf == null - and this leads to code that's either over-complex or fragile. Find ways to avoid ever assigning null, or if you can't, keep the vulnerable block of code isolated, so that nothing outside that scope needs to deal with the possibility of a null variable.

这篇关于在if语句中创建对象并在以后使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆