java
package calcvm; import java.io.*; import java.util.*; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.html.H2; import com.vaadin.flow.component.html.Label; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.router.RouteAlias; @PageTitle("Calc VM") @Route(value = "calcvm") @RouteAlias(value = "") public class CalcVMView extends VerticalLayout { public CalcVMView() { // 中置記法入力 TextField infixExpr = new TextField(); infixExpr.setWidth("400px"); Button translateButton = new Button("変換"); Button generateButton = new Button("生成"); Button executeButton = new Button("実行"); // 後置記法出力 translateButton.addClickListener(e -> { String str = infixExpr.getValue(); //strに中置記法の式を代入 Node tree = new Node(str); //二分木に格納する tree.parse(); String str2 = tree.getExpression(""); //str2に後置記法の式を代入 Label postfixOut = new Label(); postfixOut.setText(str2); String[] data2 = str2.split(","); //カンマ区切りでdata2に格納する }); // スタックマシン命令列 VerticalLayout vmInsns = new VerticalLayout(); int lg = str2.length(); String[] strArray = str2.split(""); generateButton.addClickListener(e -> { vmInsns.removeAll(); for(String s: strArray) { switch (s) { case "+":vmInsns.add(new Label("add")); break; case "-":vmInsns.add(new Label("sub")); break; case "*":vmInsns.add(new Label("mul")); break; case "/":vmInsns.add(new Label("div")); break; default: vmInsns.add(new Label("push " + s)); } } vmInsns.add( new Label("wrt"), new Label("halt") ); }); // 実行結果 Label result = new Label(); executeButton.addClickListener(e -> { result.setText(Integer.valueOf(out(data2)).toString()); }); add( new HorizontalLayout(infixExpr, translateButton, generateButton, executeButton), new H2("後置記法"), postfixOut, new H2("スタックマシン命令列"), vmInsns, new H2("実行結果"), result ); } public static int out(String[] data) { //スタック Stack<Integer> stack = new Stack<Integer>(); Cal cal = new Cal(); for(int i=0; i<data.length; i++) { if(data[i].equals("+")) { stack.add(cal.add(stack.pop(), stack.pop())); continue; } else if(data[i].equals("-")) { stack.add(cal.sub(stack.pop(), stack.pop())); continue; } else if(data[i].equals("*")) { stack.add(cal.mul(stack.pop(), stack.pop())); continue; } else if(data[i].equals("/")) { stack.add(cal.div(stack.pop(), stack.pop())); continue; } else { stack.push(Integer.parseInt(data[i])); } } return stack.pop(); } }
問題点①(解決済み??)
後置記法を出力部分のtranslateButton.addClickListener(e -> { }の中身の変数str,Node,str2,data2等で、『ラムダ式から参照されるローカル変数は、finalまたは事実上のfinalである必要があります』というエラーが出ます。どのように変更すれば良いか教えてください。
問題点②
問題点①を踏まえて、変数の定義を全てラムダ式の中にしました。すると今度はstr2,data2,postfixOutのみが『シンボルを見つけられません』というエラーになります。
ラムダ式によりそのように表示されたということであれば、無名クラスの書き方に変えてみては如何でしょうか。
先回りになりますが、
「内部クラスからアクセス可能なローカル変数が final でなければならない理由」
https://happynow.hateblo.jp/entry/20120512/1336826316
という記事や記事内リンクをご理解頂けると良いかと思います。
まだ回答がついていません
会員登録して回答してみよう