Type mismatch: cannot convert from integer to boolean

后端 未结 7 1215
忘了有多久
忘了有多久 2020-12-06 01:48
public boolean clearSelection() {
    int i = 0;
    if (!this.m_SelectedComps.isEmpty()) {
        i = 1;
        Iterator localIterator = this.m_SelectedComps.iter         


        
相关标签:
7条回答
  • 2020-12-06 02:04

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

    return i > 0 ? true : false ;
    
    0 讨论(0)
  • 2020-12-06 02:12

    Declare i as a boolean:

    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;
    }
    
    0 讨论(0)
  • 2020-12-06 02:13

    Convert int to boolean:

    return i > 0;
    
    0 讨论(0)
  • 2020-12-06 02:16

    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.

    0 讨论(0)
  • 2020-12-06 02:17

    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:

    BooleanUtils.toBoolean(int value)
    

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


    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 Dependency:

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

    Gradle Dependency:

    'org.apache.commons:commons-lang3:3.4'
    
    0 讨论(0)
  • 2020-12-06 02:19
    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);
    }
    
    0 讨论(0)
提交回复
热议问题