0

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?

1
  • "My understanding was that the ArrayList would inherit the methods of the type's class" - you mean of your ResultsEntry class? No, absolutely not. I suspect you want results.get(i).getCount(). Commented Aug 10, 2017 at 16:17

2 Answers 2

1

Your understanding is incorrect. The ArrayList contains instances of ResultsEntry, I believe you want to get the element of ResultsEntry for a given index i and then invoke getCount() on that instance. Like,

int count = results.get(i).getCount();

And, if you're using Java 8+, your entire method could be replaced with a lambda, a map and a call to sum. Like

return results.stream().mapToInt(ResultsEntry::getCount).sum();
Sign up to request clarification or add additional context in comments.

Comments

0

You frist need to get the item from arraylist before you do

.getCount();

Otherwise how should the code know which item in the list you are targeting

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.