我正在尝试理解下面给出的第一个offer()方法和第二个offer()方法之间的区别。在用Java语言进行NetBeans编程时,提示第一个offer方法中的if语句是多余的。在配置if语句之后,offer方法中的代码现在读取"return stk1.push(element).equals(element);“。"return stk1.push(element).equals(element)“这行代码到底做了什么;它是如何处理冗余的?
//FIRST - my attempt
public BinaryPollQueue()
{
stk1 = new Stack<>();
stk2 = new Stack<>();
} //BinaryPollQueue
public boolean offer(E element)
{
if(stk1.push(element).equals(element))
{
return true;
} //if
else
{
return false;
} //else
} //offer
//SECOND - configured based on hint given
public BinaryPollQueue()
{
stk1 = new Stack<>();
stk2 = new Stack<>();
} //BinaryPollQueue
public boolean offer(E element)
{
return stk1.push(element).equals(element);
} //offerhttps://stackoverflow.com/questions/47620519
复制相似问题