Q:

Sum of an array from class variable java

I just started OOP java and im struggling with getting a sum of my class type elements from an array. Can anyone help me?
hwComponents is a list type of a class HardwareComponent. Any help would be appreciated.
private Collection<HardwareComponent> hwComponents = new ArrayList<>();
public float calculatePrice() 
{

float sum=0;
for (int i=1;i < hwComponents.size(); i++) 
    sum+= hwComponents.get(i); //The method get(i) is undefined for this type
    return sum;

}

A:

A Collection doesn't have a get(index) method.
Store your ArrayList in a List variable instead:
private List<HardwareComponent> hwComponents = new ArrayList<>();

Also note that the indices of your loop should begin at 0.
As an alternative, you can use an enhanced for loop, which doesn't require the get method:
for (HardwareComponent hc : hwComponents) {
    sum+= hc.getSomeProperty(); // note that HardwareComponent cannot be added to 
              // a float (or to anything else for that matter, so you probably
              // intended to call some method of your class which returns a float
}

