我正在尝试使用后缀表达式创建表达式树。
这需要一个可以容纳树对象的堆栈。
我创建了一个通用的 Stack 类,除了 <TreeTemp>
作为类型参数。
在尝试使用以下语句初始化堆栈时,它给出“无法推断 TreeStack<> 的类型参数”错误。
private TreeStack<TreeTemp> stack1 = new TreeStack<>(new TreeTemp());
堆栈类:
public class TreeStack<T> {
private T [] stackElem;
private final int MAXSTACKSIZE;
private int top;
public TreeStack(Class<T> t) {
top = -1;
MAXSTACKSIZE = 20;
final T[] stackElem = (T[]) Array.newInstance(t, MAXSTACKSIZE);
this.stackElem = stackElem;
}
public void push(T elem) throws Exception{
if(isFull()) {
stackElem[++top] = elem;
}
else
throw new Exception("Stack is already Full");
}
public T pop() throws Exception {
if(isEmpty()) {
return stackElem[top--];
}
else
throw new Exception("Stack is Empty");
}
public boolean isEmpty() {return top == -1;}
public boolean isFull() {return top==MAXSTACKSIZE-1;}
}
Postfix.class(类具有创建树的方法)
public class PostFix {
private String expression = new String("A*B+C");
private char [] expElem = expression.toCharArray();
/*Error on below Statement*/
private TreeStack<TreeTemp> stack1 = new TreeStack<>(new TreeTemp());
public TreeTemp makeTree() throws Throwable {
try {
for(int i=0;i<expElem.length;i++) {
ExpNode eNode = new ExpNode();
eNode.setiData(expElem[i]);
TreeTemp t = new TreeTemp();
t.setRoot(eNode);
if(!Character.isLetter(expElem[i])) {
t.setLeftTree(stack1.pop());
t.setRightTree(stack1.pop());
}
stack1.push(t);
}
return stack1.pop();
}catch (Exception e) {
throw new Exception("Stack Error while creating a Tree", e);
}
}
public static void main(String[] args) throws Throwable {
PostFix pf = new PostFix();
TreeTemp t = pf.makeTree();
}
树类(我想添加到堆栈中的类型):
public class TreeTemp {
private ExpNode root;
private TreeTemp leftTree;
private TreeTemp rightTree;
/*public TreeTemp(ExpNode expNode) {
root = expNode;
}*/
public TreeTemp getLeftTree() {
return leftTree;
}
public void setLeftTree(TreeTemp leftTree) {
this.leftTree = leftTree;
}
public TreeTemp getRightTree() {
return rightTree;
}
public void setRightTree(TreeTemp rightTree) {
this.rightTree = rightTree;
}
public ExpNode getRoot() {
return root;
}
public void setRoot(ExpNode node) {
this.root = node;
}
}
谁能指点一下。
答案 0 :(得分:1)
您的 void
只有一个构造函数。这是:
TreeStack
因此,要调用它,您需要传递表示与 T 类型关联的类的类对象。因此,类 本身,而不是“T 的某个特定实例”。当您在错误行调用它时:
public TreeStack(Class<T> t) {
您正在传递一个 TreeTemp 的实例。不是“TreeTemp,类”的概念。试试private TreeStack<TreeTemp> stack1 = new TreeStack<>(new TreeTemp());
请注意,作为一般规则,传递 new TreeStack<>(TreeTemp.class);
是一种代码异味;您正在尝试使泛型成为它不是的东西(您正在尝试运行时具体化)。这在客观上是不好的:例如,这意味着您不能创建 Class<T>
,因为您仅限于泛型和 TreeStack<List<String>>
实例都可以表示事物的重叠,而这只是简单、非泛型、非原始类。
j.l.Class
看起来你想要这个类的唯一原因是确保你的数组类型正确。
这不是必需的。只需创建一个新的 final T[] stackElem = (T[]) Array.newInstance(t, MAXSTACKSIZE);
数组,并在需要返回 T 的任何时候转换为 T。现在您的 TreeStack 构造函数根本不需要参数。
查看java.util.ArrayList的源码,同意这个评价;它由一个对象数组支持,而不是一个 T[]。
答案 1 :(得分:1)
TreeStack
构造函数接受 Class<T>
,而不是 T
,因此您应该这样做:
new TreeStack<>(TreeTemp.class);
由于这是创建表达式树的练习,因此您实际上不需要从头开始实现堆栈。您应该只通过 Java Collections API 中的 ArrayDeque
接口使用 Deque
类。
private Deque<TreeTemp> stack1 = new ArrayDeque<>();
Deque
拥有您的 TreeStack
拥有的所有方法,以及更多。