0

わかりましたので、非常に単純なコンピュータークラスの割り当てがあります (配列などを横断することを示すはずです)。配列を含むバージョンとarrayListsを含むバージョンを作成する必要があったため、テスタークラスにはいくつかの静的メソッドがありますが、arrayListを使用して、オブジェクトの元のクラスからメソッドを呼び出そうとすると(getterメソッド) ) 見つからないというエラー メッセージが表示されるだけです。

これが私のコードの短縮版です:

import java.util.*; 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);

}

}

public class 候補 {

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.";
}

}

4

1 に答える 1

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);
}
}
于 2013-03-29T04:37:38.360 に答える