首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ArrayList访问器方法不返回值

ArrayList访问器方法不返回值
EN

Stack Overflow用户
提问于 2018-05-29 03:29:23
回答 2查看 579关注 0票数 0

这是我在自己的类中编写的第一个程序。它的目的是执行一次可转移的选举;现在我需要获取一个Candidate对象的ArrayList<Candidate>。在构造函数内部,该过程运行得很好;但是,一旦我尝试使用访问器方法,就会返回一个空的ArrayList。代码如下:

代码语言:javascript
复制
import java.util.*;
public class Election {

    public ArrayList<Candidate> candidates = new ArrayList<Candidate>();

    Election(int numVotes) {
        String name;
        ArrayList<Candidate> candidates = new ArrayList<Candidate>();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the candidates you want in the election.");
        System.out.println("Enter -1 once you have listed all the candidates.");
        System.out.print("Start entering candidates: ");
        name = keyboard.nextLine();
        while (!name.equals("-1")) {
            candidates.add(new Candidate(name));
            System.out.print("Ok, enter a new candidate or -1: ");
            name = keyboard.nextLine();

        }

        //Works fine here
    }

    public ArrayList<Candidate> getCandidateList() {   
        //Keeps returning empty list
        //System.out.println(candidates);
        return this.candidates;    
    }
} 
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50572508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档