由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 新手请教java generic method 调用问题
相关主题
It's a Java Bug请问有没有generic的array
问一个generic的问题吧Interview的人问我最新java 版本是多少
[转载] Java 1.5 Generic 问题extending generic class , but not mentioning its parameterized type?
What does this mean?菜鸟问关于Java Programming的问题
type erasure weird problemgenerics这样改对马?
关于==和equals问个 Generic 的问题
问两个语法问题Alternative way to swap Integer
a dump method in JavaInteger in heap
相关话题的讨论汇总
话题: letter话题: stack话题: postfix话题: infix
进入Java版参与讨论
1 (共1页)
x****a
发帖数: 1229
1
在新建的stack中,调用generic method
public E remove()方法出错。如果把E改成char,运行成功。请解惑。谢谢
/**********************************************************************
作业是实现infix, postfix互换,读取string input,把数字push to a stack,遇到
括号再pop。
要求stack必须用doublyLinkedList形式生成,就是先建DoublyLinkedList class, 再
创建stack class: DoublyLinkedList stack = new DoublyLinkedList;再创
建新class 里有infix to postfix 和 postfix to infix两个method。
我两个method里,infix to postfix, 用stack1; postfix to infix
用stack2
there is runtime error when I tried to invoke generic method of public E
remove() in the DoublyLinkedList class.
如果我改成 public char remove(),就能运行成功infix to postfix method, 但
是 postfix to infix 又发生错误,stack is Integer。
请问怎样才能做到两个不同类型的stacks in the same class 同时调用一个generic
method。
新手学数据结构,如果问题幼齿,请见谅,谢谢。
r*****l
发帖数: 2859
2
I feel the problem is not with "remove" method, but the way you call and use
it. If you can publish the code when you call "remove", plus necessary
context, and the runtime error.

【在 x****a 的大作中提到】
: 在新建的stack中,调用generic method
: public E remove()方法出错。如果把E改成char,运行成功。请解惑。谢谢
: /**********************************************************************
: 作业是实现infix, postfix互换,读取string input,把数字push to a stack,遇到
: 括号再pop。
: 要求stack必须用doublyLinkedList形式生成,就是先建DoublyLinkedList class, 再
: 创建stack class: DoublyLinkedList stack = new DoublyLinkedList;再创
: 建新class 里有infix to postfix 和 postfix to infix两个method。
: 我两个method里,infix to postfix, 用stack1; postfix to infix
: 用stack2

x****a
发帖数: 1229
3
// public void postfix (String input)
// {
// Stack s1 = new Stack();
// String output = "";//to store the postfix expression
//
// for (int i =0; i // {
// //get the single letter
// char letter = input.charAt(i);
//
// //convert infix to postfix
// if (letter =='1'||letter =='2'|| letter =='3'||letter =='4'||
letter =='5'||letter =='6'||
// letter =='7'||letter =='8'||letter =='9')
// {
// output+=letter;
// }
// else if (letter == '(')
// {
// //ignore it
// }
// else if (letter == '*' || letter == '+' || letter == '-' ||
letter == '/')
// {
// if(s1.isEmpty())
// {
// s1.push(letter);//make the min size of the sack is 1
to avoid EmptyStackException
// s1.push(letter);
// }
// else
// {
// s1.push(letter);
// }
// }
// else if (letter == ')')
// {
// do
// {
// output+=s1.pop();
//
// }while(s1.size()>1);
// }
// }
// System.out.println(output);
// }
x****a
发帖数: 1229
4
in stack class, pop() method is:
public class Stack
{
private int size;
private DoublyLinkedList stack;
public Stack()
{
this.stack = new DoublyLinkedList ();
this.size = 0;
}//default constructor
....
public int pop()
{
if (this.isEmpty())
{
throw new EmptyStackException();
}
this.size--;
return this.stack.remove(this.size());
}
}
x****a
发帖数: 1229
5
DoublyLinkedList class:
public class DoublyLinkedList
{
public E remove (int index) throws IndexOutOfBoundsException
{
if (index<0 || index>= this.size)
throw new IndexOutOfBoundsException();

DLNode cursor = (DLNode)this.head.getSuccessor();

if (index>0)
cursor = this.find(index-1);

DLNode target = (DLNode)cursor.getSuccessor();

(target.getPredecessor()).setSuccessor((DLNode)(target.
getSuccessor()));
((DLNode)target.getSuccessor()).setPredecessor(target.
getPredecessor());

E element = (E)target.getElement();
target.setSuccessor (null);
target.setPredecessor(null);
target.setElement(null);
size--;
return element;
}

public E remove (E target)
{
return this.remove(this.findIndex(target));
}//ending remove method
}
N***m
发帖数: 4460
6
你能不能把问题问的简化点?大段大段的描述看着头晕。
你就描述你想实现什么就行了,背景啥的可以暂时忽略,非用不可的时候你再说。

【在 x****a 的大作中提到】
: DoublyLinkedList class:
: public class DoublyLinkedList
: {
: public E remove (int index) throws IndexOutOfBoundsException
: {
: if (index<0 || index>= this.size)
: throw new IndexOutOfBoundsException();
:
: DLNode cursor = (DLNode)this.head.getSuccessor();
:

x****a
发帖数: 1229
7
runtime error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from E to int
at Stack.pop(Stack.java:40)
at ArithmeticEvaluator.postfix(ArithmeticEvaluator.java:68)
at ArithmeticEvaluatorDriver.main(ArithmeticEvaluatorDriver.java:15)
x****a
发帖数: 1229
8
在新建的stack中,引用generic method
public E remove()方法出错。
没办法,怕问题说不清楚,才贴的大段。老师写的code,都是linked list型的,一个
扯着一个的。

【在 N***m 的大作中提到】
: 你能不能把问题问的简化点?大段大段的描述看着头晕。
: 你就描述你想实现什么就行了,背景啥的可以暂时忽略,非用不可的时候你再说。

l*********s
发帖数: 5409
9
the problem is that Java generic must be object, not prototype like int,
float, etc. You shall use Integer class .

【在 x****a 的大作中提到】
: runtime error:
: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
: Type mismatch: cannot convert from E to int
: at Stack.pop(Stack.java:40)
: at ArithmeticEvaluator.postfix(ArithmeticEvaluator.java:68)
: at ArithmeticEvaluatorDriver.main(ArithmeticEvaluatorDriver.java:15)

x****a
发帖数: 1229
10
I see. It compiled successfully when I changed the pop() method in the stack
class with generic type.thanks.
public int pop()>>>>>>>>>>>public E pop(), then invoke public E remove().
One more question, how to deal with null pointer exception problem with
doublyLinkedList- type stack after I pop all the elements.
It shows error at "((DLNode)target.getSuccessor()).setPredecessor(target.
getPredecessor());"
My silly solution is to push one more element when stack.isEmpty to avoid
the situation. any other idea? thanks.
r*****l
发帖数: 2859
11
You need to check null to avoid NPE.

stack
target.

【在 x****a 的大作中提到】
: I see. It compiled successfully when I changed the pop() method in the stack
: class with generic type.thanks.
: public int pop()>>>>>>>>>>>public E pop(), then invoke public E remove().
: One more question, how to deal with null pointer exception problem with
: doublyLinkedList- type stack after I pop all the elements.
: It shows error at "((DLNode)target.getSuccessor()).setPredecessor(target.
: getPredecessor());"
: My silly solution is to push one more element when stack.isEmpty to avoid
: the situation. any other idea? thanks.

1 (共1页)
进入Java版参与讨论
相关主题
Integer in heaptype erasure weird problem
Which WS stack should I use with JBoss AS ?关于==和equals
a fun coding question问两个语法问题
出个简单题,看你Java APi熟悉到什么程度a dump method in Java
It's a Java Bug请问有没有generic的array
问一个generic的问题吧Interview的人问我最新java 版本是多少
[转载] Java 1.5 Generic 问题extending generic class , but not mentioning its parameterized type?
What does this mean?菜鸟问关于Java Programming的问题
相关话题的讨论汇总
话题: letter话题: stack话题: postfix话题: infix