英文:
in my if statement my variable isn't found?
问题
在我的代码中,在我的if语句中,Objects.deepEquals部分显示"无法找到符号 - 变量Objects"。我也有一个驱动程序代码,但我不知道那是否会有帮助。
public class RunnyStack <Base>{
class Run{
final Base object;
int length;
Run next;
public Run(Base object) {
this.object = object;
next = null;
length = 1;
}
public Run(Base object, Run next) {
this.object = object;
this.next = next;
length = 1;
}
}
Run first;
int countDepth;
int countRun;
public RunnyStack() {
first = null;
countDepth = countRun = 0;
}
public int depth(){
return countDepth;
}
public boolean isEmpty(){
return first == null;
}
public Base peek(){
if (first!=null)
return first.object;
throw new IllegalStateException();
}
public void pop(){
if (first!=null) {
first.length--;
countDepth --;
if (first.length == 0){
first = first.next;
countRun--;
}
}
else
throw new IllegalStateException();
}
public void push(Base object){
if (first == null){
first = new Run(object);
countDepth++;
countRun++;
}
else {
if (Objects.deepEquals(first.object, object))
first.length++;
else {
first = new Run(object, first);
countRun++;
}
countDepth++;
}
}
public int runs(){
return countRun;
}
}
以上是您提供的代码的翻译部分。
英文:
in my code in my if statement the Objects.deepEquals it says "cannot find symbol - variable Objects" I have a driver code as well but I do not know if that would help.
I was wondering if anyone would be able to help
public class RunnyStack <Base>{
class Run{
final Base object;
int length;
Run next;
public Run(Base object) {
this.object = object;
next = null;
length = 1;
}
public Run(Base object, Run next) {
this.object = object;
this.next = next;
length = 1;
}
}
Run first;
int countDepth;
int countRun;
public RunnyStack() {
first = null;
countDepth = countRun = 0;
}
public int depth(){
return countDepth;
}
public boolean isEmpty(){
return first == null;
}
public Base peek(){
if (first!=null)
return first.object;
throw new IllegalStateException();
}
public void pop(){
if (first!=null) {
first.length--;
countDepth --;
if (first.length == 0){
first = first.next;
countRun--;
}
}
else
throw new IllegalStateException();
}
public void push(Base object){
if (first == null){
first = new Run(object);
countDepth++;
countRun++;
}
else {
if (Objects.deepEquals(first.object, object))
first.length++;
else {
first = new Run(object, first);
countRun++;
}
countDepth++;
}
}
public int runs(){
return countRun;
}
}
To post this it is asking me to type more. So this part here is only to fill up space. this text is irrelevant.
答案1
得分: 2
你没有展示给我们您的import
语句,但是Objects
不位于java.lang
中,并且不会被默认导入。添加以下代码:
import java.util.Objects;
或者将以下代码:
if (Objects.deepEquals(first.object, object))
改为:
if (java.util.Objects.deepEquals(first.object, object))
另外,请在可选情况下也使用{}
。
英文:
You don't show us your import
(s), but Objects
is not in java.lang
and it is not imported by default. Add
import java.util.Objects;
or change
if (Objects.deepEquals(first.object, object))
to
if (java.util.Objects.deepEquals(first.object, object))
Also, please use {}
even when optional.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论