I'm trying to learn a bit more about how ArrayList types work when using a custom class and have run into a problem I don't quite understand. I have the public get methods set in my type class, but can't get them to work when calling them for my ArrayList. Here are simplified versions of the classes:
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 + ">");
}
}
Then the next class:
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;
}
}
The problem I'm having is that results.getCount(i); gives an error that getCount is not defined for type ArrayList<ResultsEntry>.
My understanding was that the ArrayList would inherit the methods of the type's class. Any insight into what's happening here?
ResultsEntryclass? No, absolutely not. I suspect you wantresults.get(i).getCount().