カスタムクラスを使用しているときにArrayListの型がどのように機能するかについて、私はもう少し詳しく学んでいます。私はパブリックメソッドを自分の型クラスに設定するが、私のArrayListのためにそれらを呼び出すときに動作するようにすることはできません。ArrayListオブジェクトの変数へのアクセス - タイプクラスのゲッターが機能しない
public class ResultsEntry {
//Create instance private variables count (int) and target (char)
private Integer count;
private char target;
//Create a single constructor with the two values
public ResultsEntry (Integer count, char target)
{
this.count = count;
this.target = target;
}
//Public get methods for count and target
public Integer getCount()
{
return count;
}
public char getTarget()
{
return target;
}
//Public toString method that returns a string in the format <target, count>
public String toString() {
return ("<" + target + ", " + count + ">");
}
}
その後、次のクラス:ここではクラスの単純化したバージョンです
import java.util.ArrayList;
public class SharedResults {
//Create private instance variable - results (ArrayList of ResultsEntry type)
private static ArrayList<ResultsEntry> results = new ArrayList<ResultsEntry>();
//A default constructor that initializes the above data structure
public SharedResults (Integer resultsCount, char resultsTarget)
{
Integer sharedResultsCount = resultsCount;
char sharedResultsTarget = resultsTarget;
results.add(new ResultsEntry(sharedResultsCount, sharedResultsTarget));
}
/*
* getResult method with no arguments returns sum of the count entry values in the
* shared results data structure.
*/
public static Integer getResults()
{
Integer sum = 0;
for (int i = 0; i < results.size(); i++) {
System.out.println("getResults method input "+ results.(i));
Integer input = results.getCount(i);
sum = input + sum;
/*
*Some code here that adds new count results to the counts in all
*other array elements
*/
}
return sum;
}
}
私がいる問題はresults.getCount(i);
は同様にgetCountは、タイプArrayList<ResultsEntry>
のために定義されていないエラーを与えるということです。
私の理解は、ArrayListが型のクラスのメソッドを継承するということでした。何が起こっているのかについての洞察はありますか?
「私の理解では、あなたが
を行う前に、コードを使用すると、ターゲットとしているリスト内のどの項目を知っている必要がありそうでないかのArrayListからアイテムを取得するための延期が必要だったことArrayListは型のクラスのメソッドを継承します " - あなたの' ResultsEntry'クラスの意味ですか?いいえ、絶対にありません。私は 'results.get(i).getCount()'がほしいと思う。 –