2013-03-29 2 views
0

좋아, 그래서 내 컴퓨터 클래스에 대한 할당이 매우 간단합니다. (배열과 물건을 가로 지르는 것을 보여 주기로되어 있습니다.) 배열과 버전을 arrayLists와 함께 만들어야 했으므로 테스터 클래스에 정적 메서드가 몇 개 있습니다.하지만 arrayList를 사용하면 객체가있는 클래스에서 메서드를 호출하려고합니다 (getter 메서드) 내가 얻을 수있는 메시지는 찾을 수 없다는 오류 메시지입니다. . 여기 객체의 ArrayList 도움이되는 자바

내 코드의 단축 버전입니다 :

수입 java.util의 *; import java.util.List;

공용 클래스 testCandidate2 {

public static int getTotal(ArrayList election) 
{ 
    int total = 0; 
    for(int b = 0; b <election.size(); b++) 
      total += election.getNumVotes(); 
    return total; 
} 


public static void main(String[] args) 
{ 
    int totalVotes; 
    List <Candidate> election = new ArrayList<Candidate>(5); 
    election.add() = new Candidate(5000, "John Smith"); 

    totalVotes = getTotal(election); 

} 

}

공용 클래스 후보 {

private String name; 
private int numVotes; 

Candidate(int nv, String n) 
{ 
    name = n; 
    numVotes = nv; 
} 

public String getName() 
{ 
    return name; 
} 

public int getNumVotes() 
{ 
    return numVotes; 
} 

public String toString() 
{ 
    return name + " recieved " + numVotes + " votes."; 
} 

}

+0

이 줄 바꾸기 election.add() = 새 후보 (5000, "John Smith"); 선거와 함께. 추가 (새 후보 (5000, "존 스미스")); – Satya

+0

감사합니다. 더 낫지 만 여전히 "total + = election.getNumVotes();"에 오류 메시지가 표시됩니다. 나는 또한 "total + = election.get (b) .getNumVotes();"을 시도했다. 와 같은 오류 메시지가 나타납니다. "심볼 - 메소드 getNumVotes()를 찾을 수 없습니다." – user2223159

+0

[내재적 인 ArrayList] 튜토리얼 (http://volodial.blogspot.com/2013/07/internal-life -of-arraylist-in-java.html) –

답변

0

이 시도 :

import java.util.*; 
//import java.util.List; 

public class testCandidate2 { 

public static int getTotal(ArrayList election) 
{ 
    int total = 0; 
    for(int b = 0; b <election.size(); b++) 
     { 
     Candidate ele = (Candidate) election.get(b); 
     // System.out.println(ele.getNumVotes()); 
     total += ele.getNumVotes(); 
     //System.out.println(o.getNumVotes()); 
    } 
     //System.out.println((Candidate) (election.get(b).getNumVotes()); 
      //.getNumVotes(); 
    return total; 
} 


public static void main(String[] args) 
{ 
    int totalVotes; 
    ArrayList <Candidate> election = new ArrayList<Candidate>(5); 
    election.add(new Candidate(5000, "John Smith")); 

    totalVotes = getTotal(election); 
    System.out.println(totalVotes); 
} 
} 
+0

예! 그것은 작동합니다. 감사합니다. 감사합니다. – user2223159

관련 문제