类型不匹配:不能从整数转换为布尔

时间:2023-01-12 13:10:52
public boolean clearSelection() {
    int i = 0;
    if (!this.m_SelectedComps.isEmpty()) {
        i = 1;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
            ((AnnotComponent) localIterator.next()).remove();
        this.m_SelectedComps.clear();
    }

    return i;
}

How to convert the integer to boolean?

如何将整数转换成布尔值?

6 个解决方案

#1


36  

Try using this return

试着用这个返回

return i == 1;

or just use a boolean to start (with a better name):

或者用布尔值开头(用更好的名字):

 public boolean clearSelection()
  {
    boolean flag = false;
    if (!this.m_SelectedComps.isEmpty())
    {
      flag = true;
      Iterator localIterator = this.m_SelectedComps.iterator();
      while (localIterator.hasNext())
        ((AnnotComponent)localIterator.next()).remove();
      this.m_SelectedComps.clear();
    }
    return flag;
  }

It continues to mystify me why people use i -- a horrible variable name. Looks like 1 and does not convey any meaning.

它继续迷惑我为什么人们使用i——一个可怕的变量名。看起来像1,没有任何意义。

#2


14  

May be you can just modify your return statement without much change to code as below:

您可以修改您的返回语句,而不需要对代码做太多修改,如下所示:

return i > 0 ? true : false ;

#3


13  

I know this thread is old but wanted add some code that helped me and might help others searching this...

我知道这个线程是旧的,但想要添加一些代码,帮助我和可能帮助其他人搜索这个……

You could use org.apache.commons.lang api to convert an int to boolean using the BooleanUtils class:

您可以使用org.apache.commons.lang api使用BooleanUtils类将int转换为boolean:

BooleanUtils.toBoolean(int value)

"Converts an int to a boolean using the convention that zero is false." (Javadocs)

“使用0为假的约定将int转换为boolean。”(javadoc)


Here are the Maven & Gradle dependencies, just make sure you check you're using the latest version on the link http://mvnrepository.com/artifact/org.apache.commons/commons-lang3

下面是Maven和Gradle依赖项,请确保在链接http://mvnrepository.com/artifact/org.apache.commons/commons-lang3中使用最新版本。

Maven Dependency:

Maven的依赖:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

Gradle Dependency:

Gradle依赖性:

'org.apache.commons:commons-lang3:3.4'

#4


2  

Convert int to boolean:

将整数转换为布尔值:

return i > 0;

#5


1  

Declare i as a boolean:

将i声明为布尔值:

public boolean clearSelection()
{
    boolean i = false;
    if (!this.m_SelectedComps.isEmpty())
    {
        i = true;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
          ((AnnotComponent)localIterator.next()).remove();
        this.m_SelectedComps.clear();
    }
    return i;
}

#6


0  

public boolean clearSelection(){
    int i = 0;
    if (!this.m_SelectedComps.isEmpty())
    {
        i = 1;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
            ((AnnotComponent)localIterator.next()).remove();
        this.m_SelectedComps.clear();
     }
     return (i!=0);
}

#1


36  

Try using this return

试着用这个返回

return i == 1;

or just use a boolean to start (with a better name):

或者用布尔值开头(用更好的名字):

 public boolean clearSelection()
  {
    boolean flag = false;
    if (!this.m_SelectedComps.isEmpty())
    {
      flag = true;
      Iterator localIterator = this.m_SelectedComps.iterator();
      while (localIterator.hasNext())
        ((AnnotComponent)localIterator.next()).remove();
      this.m_SelectedComps.clear();
    }
    return flag;
  }

It continues to mystify me why people use i -- a horrible variable name. Looks like 1 and does not convey any meaning.

它继续迷惑我为什么人们使用i——一个可怕的变量名。看起来像1,没有任何意义。

#2


14  

May be you can just modify your return statement without much change to code as below:

您可以修改您的返回语句,而不需要对代码做太多修改,如下所示:

return i > 0 ? true : false ;

#3


13  

I know this thread is old but wanted add some code that helped me and might help others searching this...

我知道这个线程是旧的,但想要添加一些代码,帮助我和可能帮助其他人搜索这个……

You could use org.apache.commons.lang api to convert an int to boolean using the BooleanUtils class:

您可以使用org.apache.commons.lang api使用BooleanUtils类将int转换为boolean:

BooleanUtils.toBoolean(int value)

"Converts an int to a boolean using the convention that zero is false." (Javadocs)

“使用0为假的约定将int转换为boolean。”(javadoc)


Here are the Maven & Gradle dependencies, just make sure you check you're using the latest version on the link http://mvnrepository.com/artifact/org.apache.commons/commons-lang3

下面是Maven和Gradle依赖项,请确保在链接http://mvnrepository.com/artifact/org.apache.commons/commons-lang3中使用最新版本。

Maven Dependency:

Maven的依赖:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

Gradle Dependency:

Gradle依赖性:

'org.apache.commons:commons-lang3:3.4'

#4


2  

Convert int to boolean:

将整数转换为布尔值:

return i > 0;

#5


1  

Declare i as a boolean:

将i声明为布尔值:

public boolean clearSelection()
{
    boolean i = false;
    if (!this.m_SelectedComps.isEmpty())
    {
        i = true;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
          ((AnnotComponent)localIterator.next()).remove();
        this.m_SelectedComps.clear();
    }
    return i;
}

#6


0  

public boolean clearSelection(){
    int i = 0;
    if (!this.m_SelectedComps.isEmpty())
    {
        i = 1;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
            ((AnnotComponent)localIterator.next()).remove();
        this.m_SelectedComps.clear();
     }
     return (i!=0);
}