Java 访问ArrayList对象类型类getter中的变量不起作用

Java 访问ArrayList对象类型类getter中的变量不起作用,java,class,inheritance,arraylist,types,Java,Class,Inheritance,Arraylist,Types,我试图学习更多关于ArrayList类型在使用自定义类时如何工作的知识,但遇到了一个我不太理解的问题。我在类型类中设置了public get方法,但在为ArrayList调用它们时无法使它们工作。以下是这些类的简化版本: public class ResultsEntry { //Create instance private variables count (int) and target (char) private Integer count; private char target;

我试图学习更多关于ArrayList类型在使用自定义类时如何工作的知识,但遇到了一个我不太理解的问题。我在类型类中设置了public 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 + ">");
}   
}
公共类结果中心{
//创建实例私有变量count(int)和target(char)
私有整数计数;
私有字符目标;
//创建一个具有两个值的构造函数
公共结果中心(整数计数,字符目标)
{
this.count=计数;
this.target=目标;
}
//count和target的公共get方法
公共整数getCount()
{
返回计数;
}
公共字符getTarget()
{
回报目标;
}
//返回格式为的字符串的Public-toString方法
公共字符串toString(){
返回(“”);
}   
}
然后下一节课:

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;
}
}
import java.util.ArrayList;
公共类共享结果{
//创建私有实例变量-results(ResultEntry类型的ArrayList)
私有静态ArrayList results=新ArrayList();
//初始化上述数据结构的默认构造函数
公共共享结果(整数结果计数、字符结果目标)
{
整数sharedResultsCount=resultsCount;
char sharedResultsTarget=resultsTarget;
添加(新结果中心(sharedResultsCount、SharedResultsStarGet));
}
/* 
*不带参数的getResult方法返回
*共享结果数据结构。
*/
公共静态整数getResults()
{
整数和=0;
对于(int i=0;i
我遇到的问题是
results.getCount(I)
给出了一个错误,即没有为类型
ArrayList
定义getCount


我的理解是ArrayList将继承类型类的方法。对这里发生的事情有什么见解吗?

你的理解是错误的。
ArrayList
包含
resultentry
的实例,我相信您希望获取给定索引
I
resultentry
元素,然后对该实例调用
getCount()
。像

int count = results.get(i).getCount();
而且,如果您使用的是Java8+,那么整个方法可以替换为lambda、map和对
sum
的调用。像

return results.stream().mapToInt(ResultsEntry::getCount).sum();

您首先需要从arraylist获取项目,然后才能执行此操作

.getCount();

否则,代码如何知道您针对的是列表中的哪一项?

“我的理解是ArrayList将继承类型类的方法”-您指的是您的
结果中心
类?不,绝对不是。我怀疑您想要
结果。get(I).getCount()