错误“无法找到符号 - 方法isCompleted()

时间:2013-12-01 15:08:34

标签: java arraylist count

我不断收到此错误“无法找到符号 - 方法isCompleted()”但我已经声明了它。我做错了什么???

private boolean isCompleted;

public boolean isCompleted()
{
return isCompleted = true;
}

public int getJobsWaiting()
{        
    int count = 0;
    int i = 0;
    while (i < jobList.size())
    {
        if(!jobList.get(i).isCompleted())
        {
            count = count + 1;
        }
        i = i+1;
    }
    return count;
}

1 个答案:

答案 0 :(得分:1)

import java.util.ArrayList;
import java.util.List;

public class Jobs {
    List<Job> jobList = null;

    public void createJobs(){
        jobList = new ArrayList<Job>();

        // create three jobs
        for(int index = 0; index < 3; index++){
            jobList.add(new Job());
        }

    }

    public List<Job> getJobs(){
        return jobList;
    }

    public int getJobsWaiting()
    {        
        int count = 0;
        int i = 0;
        while (i < jobList.size())
        {
            if(!jobList.get(i).isCompleted())
            {
                count = count + 1;
            }
            i = i+1;
        }
        return count;
    }


    class Job {

        private boolean isCompleted;


        public boolean isCompleted()
        {
            return isCompleted;
        }

    }

    public static void main(String[] args) {
        Jobs myJob = new Jobs();
        myJob.createJobs();

        System.out.println(myJob.getJobsWaiting()); // return 3

        List<Job> jobs = myJob.getJobs();

        for(int index = 0; index < 3; index++){
            System.out.println(jobs.get(index).isCompleted());
        }
    }
}

jobList.get(i)应返回包含isCompleted()的对象。所以请确保你正确地调用了正确的对象。