在我的if语句中,我的变量找不到?

huangapple go评论117阅读模式
英文:

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 &lt;Base&gt;{
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.

huangapple
  • 本文由 发表于 2020年10月25日 23:28:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64525442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定