访问ArrayList对象中的变量 - 键入类getter不起作用

时间:2017-08-10 16:15:22

标签: java class inheritance arraylist types

我正在尝试更多地了解ArrayList类型在使用自定义类时的工作方式,并遇到了一个我不太了解的问题。我在我的类型类中设置了公共get方法,但是在为我的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);给出了一个错误,即没有为类型ArrayList<ResultsEntry>定义getCount。

我的理解是ArrayList将继承类型类的方法。对这里发生的事情的任何见解?

0 个答案:

没有答案
相关问题