Redian新闻
>
PhD student positions in ME with full financial support ava (转载)
avatar
PhD student positions in ME with full financial support ava (转载)# Engineering - 工程
x*a
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。
新手学数据结构,如果问题幼齿,请见谅,谢谢。
avatar
a*3
2
【 以下文字转载自 Chemistry 讨论区 】
发信人: april123 (april), 信区: Chemistry
标 题: PhD student positions in ME with full financial support available
发信站: BBS 未名空间站 (Wed Nov 28 14:05:41 2012, 美东)
PhD student positions with full financial support are available starting
Summer (June) 2013 in Prof. Wang’s group in the Department of Mechanical
Engineering at Louisiana State University.
The group’s research centers around new-generation energy materials and
devices. Research interests include nanomaterials synthesis, solar cells,
lithium-ion batteries, photocatalysis, sol-gel processing, atomic layer
deposition, and electrochemical synthesis. Details can be found on the
following web:
http://appl003.lsu.edu/mech/mechweb.nsf/$Content/Ying+Wang?OpenDocument
Candidates with background in chemistry, materials science, mechanical
engineering, or chemical engineering are encouraged to apply. Candidates
with master degree are preferred, though not required. Interested
candidates are encouraged to email their CV in PDF to Prof. Wang at [email protected]
lsu.edu.
avatar
r*l
3
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

avatar
x*a
4
// 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);
// }
avatar
x*a
5
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());
}
}
avatar
x*a
6
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
}
avatar
N*m
7
你能不能把问题问的简化点?大段大段的描述看着头晕。
你就描述你想实现什么就行了,背景啥的可以暂时忽略,非用不可的时候你再说。

【在 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();
:

avatar
x*a
8
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)
avatar
x*a
9
在新建的stack中,引用generic method
public E remove()方法出错。
没办法,怕问题说不清楚,才贴的大段。老师写的code,都是linked list型的,一个
扯着一个的。

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

avatar
l*s
10
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)

avatar
x*a
11
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.
avatar
r*l
12
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。