为什么我的代码没有显示这两个Arraylists?

时间:2015-12-14 19:02:27

标签: java oop arraylist

我需要显示一个ArrayList,其中列出了个人可能拥有的大学学位。

在另一个ArrayList中,我需要显示符合公司要求的大学学位要求的个人名单。

在循环中尝试了一个system.out,它甚至都没有访问for循环。

* for循环的2个方法在这个类的末尾

import java.util.ArrayList;

public class RecruitingCompany {


private String companyName;
private int phoneNumber;
private String address;
private ArrayList<CollegeDegree> collegeDegreeList = new ArrayList<>();
private ArrayList<Candidate> candidateList = new ArrayList<>();

private Candidate candidate;
private Requirement academicDegree;

public RecruitingCompany(){
    /*main constructor*/
}


public RecruitingCompany(String companyName, int phoneNumber, String address){
    this.companyName = companyName;
    this.phoneNumber = phoneNumber;
    this.address = address;
}

public String getCompanyName() {
    return companyName;
}

public void setCompanyName(String companyName) {
    this.companyName = companyName;
}

public int getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(int phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public ArrayList<CollegeDegree> getCollegeDegreeList() {
    return collegeDegreeList;
}

public void setCollegeDegreeList(ArrayList<CollegeDegree> collegeDegreeList) {
    this.collegeDegreeList = collegeDegreeList;
}

public ArrayList<Candidate> getCandidateList() {
    return candidateList;
}

public void setCandidateList(ArrayList<Candidate> candidateList) {
    this.candidateList = candidateList;
}

public Candidate getCandidate() {
    return candidate;
}

public void setCandidate(Candidate candidate) {
    this.candidate = candidate;
}

public Requirement getAcademicDegree() {
    return academicDegree;
}

public void setAcademicDegree(Requirement academicDegree) {
    this.academicDegree = academicDegree;
}

public String showCandidateCollegeDegrees(){
    String degree = "Candidato: " + candidate.getName() + "\n";

    for (CollegeDegree cd: this.collegeDegreeList){
        degree += cd.toString();
    }
    return degree;
}

public String selectByCollegeDegree(){
    String person = "Título: " + academicDegree.getDegree() + "\n";

    for (Candidate c: this.candidateList){
            person += c.toString();
    }
    return person;
}

}

Tester类

public class Tester {

public static void main(String[] args) {

    AvailableJob availableJob = new AvailableJob(2005, 2011, "Contador", 444464444, "Metalco", "del poste de luz, 50m oeste", 550.000);
    Candidate candidate = new Candidate("Asdrubal", 888888888, "Asdrubal@yahoo.com", "Bachillerato"); 
    CollegeDegree collegeDegree = new CollegeDegree("Bachillerato Administracion", 2003, "Universidad Aguila Calva");
    RecruitingCompany recruitingCo = new RecruitingCompany();
    Requirement requirement = new Requirement("Bachillerato", 4);

    recruitingCo.setCandidate(candidate); 
    recruitingCo.setAcademicDegree(requirement); 
    availableJob.setRequirement(requirement);

    System.out.println(recruitingCo.showCandidateCollegeDegrees());
    System.out.println();
    System.out.println(recruitingCo.selectByCollegeDegree());
    System.out.println();
    System.out.println(availableJob.showRequirement());

    //System.out.print(recruitingCo.getCandidateList());
}

}

2 个答案:

答案 0 :(得分:2)

您没有将新的候选人或学位添加到arraylists。更改它将起作用的以下方法,

public void setAcademicDegree(Requirement academicDegree) {
    this.academicDegree = academicDegree;
    collegeDegreeList.add(academicDegree);
}

public void setCandidate(Candidate candidate) {
    this.candidate = candidate;
    candidateList.add(candidate);
}

现在,当您设置新的Candidate对象或CollegeDegree对象时,它将自动添加到列表中。

答案 1 :(得分:0)

在您的代码中,您需要进行一些更改:

  1. setCollegeDegreeList()setCandidateList()setCandidate()方法 应更改为addCollegDegree()addCandidate()
  2. recruitingCo.setCandidate(candidate)应该替换为 recruitingCo.addCandidate(candidate);
  3. 需要在main();
  4. 中添加recruitingCo.addCollegeDegree(collegeDegree);

    您可以获得以下代码:

    import java.util.ArrayList;
    
    public class RecruitingCompany {
    
    
    private String companyName;
    private int phoneNumber;
    private String address;
    private ArrayList<CollegeDegree> collegeDegreeList = new ArrayList<>();
    private ArrayList<Candidate> candidateList = new ArrayList<>();   
    private Requirement academicDegree;
    
    public RecruitingCompany(){
        /*main constructor*/
    }
    
    
    public RecruitingCompany(String companyName, int phoneNumber, String address){
        this.companyName = companyName;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }
    
    public String getCompanyName() {
        return companyName;
    }
    
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    
    public int getPhoneNumber() {
        return phoneNumber;
    }
    
    public void setPhoneNumber(int phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    
    public String getAddress() {
        return address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    public ArrayList<CollegeDegree> getCollegeDegreeList() {
        return collegeDegreeList;
    }
    
    public ArrayList<Candidate> getCandidateList() {
        return candidateList;
    }
    
    public void addCollegeDegree(CollegeDegree collegeDegree) {
        this.collegeDegreeList.add(collegeDegree);
    }
    
    public void addCandidate(Candidate candidate) {
        this.candidateList.add(candidate);
    }
    
    public Requirement getAcademicDegree() {
        return academicDegree;
    }
    
    public void setAcademicDegree(Requirement academicDegree) {
        this.academicDegree = academicDegree;
    }
    
    public String showCandidateCollegeDegrees(){
        String degree = "Candidato: " + candidate.getName() + "\n";
    
        for (CollegeDegree cd: this.collegeDegreeList){
            degree += cd.toString();
        }
        return degree;
    }
    
    public String selectByCollegeDegree(){
        String person = "Título: " + academicDegree.getDegree() + "\n";
    
        for (Candidate c: this.candidateList){
                person += c.toString();
        }
        return person;
    }
    
    }
    

    <强>测试仪:

    public class Tester {
    
    public static void main(String[] args) {
    
        AvailableJob availableJob = new AvailableJob(2005, 2011, "Contador", 444464444, "Metalco", "del poste de luz, 50m oeste", 550.000);
        Candidate candidate = new Candidate("Asdrubal", 888888888, "Asdrubal@yahoo.com", "Bachillerato"); 
        CollegeDegree collegeDegree = new CollegeDegree("Bachillerato Administracion", 2003, "Universidad Aguila Calva");
        RecruitingCompany recruitingCo = new RecruitingCompany();
        Requirement requirement = new Requirement("Bachillerato", 4);
    
        recruitingCo.addCandidate(candidate); 
        recruitingCo.addСollegeDegree(collegeDegree); 
    
        recruitingCo.setAcademicDegree(requirement); 
        availableJob.setRequirement(requirement);
    
        System.out.println(recruitingCo.showCandidateCollegeDegrees());
        System.out.println();
        System.out.println(recruitingCo.selectByCollegeDegree());
        System.out.println();
        System.out.println(availableJob.showRequirement());
    
        //System.out.print(recruitingCo.getCandidateList());
    }
    
    }