Tuesday, 10 September 2013

How to call method which is placed in class stored in array of objects?

How to call method which is placed in class stored in array of objects?

I have class Jorney where you have several variables and some methods that
returns those variables values.
public class Jorney {
private int timeH;
public Jorney(int timeH)
{
this.timeH=timeH;
}
private int getTimeH()
{
return townFrom;
}
}
and than i have another class where i create ArrayList of objects
ArrayList<Object> myJorney = new ArrayList<Object>();
now when I add some objects to ArrayList my aim is to go through all those
'jorneys'(objects) call getTimeH() method and store its parameters in
variables.
I use iterator to go from one object to another but dunno how to call the
method.
private int totalTime()
{
Iterator<Object> i = myJorney.iterator();
int sum;
while (i.hasNext()) {
Object j = i.next();
int hour= j.getTimeH();
sum=sum+hour;
}
return sum;
I am wondering if someone could help me with that?

No comments:

Post a Comment