2015-12-14 71 views
2

我需要顯示一個ArrayList,列出一個人可能擁有的大學學位。爲什麼我的代碼不顯示這2個Arraylist?

在另一個ArrayList中,我需要顯示符合公司要求的大學學位要求的個人列表。

在循環內嘗試了一個system.out,它甚至沒有訪問for循環。

* 2種方法與for循環在這個類

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, "[email protected]", "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()); 
} 

} 
+4

你從不向這些'ArrayList'添加任何東西。 – resueman

+0

如何?我將它們添加到測試儀中。 – lingote

+1

你正在設置'candidate'和'academicDegree'字段,但是那些不會修改'List'。如果你想,你需要在列表中調用'add'方法(例如,在你的'setCandidate'方法內部)。 – resueman

回答

2

您沒有添加新的候選人或程度的的ArrayList。更改以下方法,將工作,

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

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

現在,當您設置一個新的候選對象或對象CollegeDegree將被自動添加到列表中。

+0

是的,這工作。正如resueman在另一篇文章中所說的那樣,我沒有給列表添加任何內容。我做了一些其他的改變,使一切工作,但這個職位讓我走上正軌。謝謝 – lingote

+0

沒問題,謝謝你的確認雖然... :) –

0

在你的代碼的最後,你需要做一些改變:

  1. setCollegeDegreeList()setCandidateList()setCandidate()方法 應該是CH anged到addCollegDegree()addCandidate()
  2. recruitingCo.setCandidate(candidate)應更換到 recruitingCo.addCandidate(candidate);
  3. 需要添加在主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, "[email protected]", "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()); 
} 

} 
+2

代碼轉儲答案沒有用,因爲它不清楚你實際改變了什麼。請描述更改內容。另外,複製你沒有改變的代碼是很有用的 - 如果它不是問題的一部分,不要將它作爲答案的一部分。 –

+0

我已經修好了 –

+0

他在測試儀類中增加了2行。 – lingote