I have a ArrayList that's not specified as double/float but it contains double float values as 22.33
, 12.56
, 34
, 21
...
I actually have two that I use when I read from a file to savee the data to. They look like this
static List<MySample> data = new ArrayList<MySample>();
static ArrayList list = new ArrayList();
With ArrayList<Double> list = new ArrayList();
I can use a simple
loop like:
double sum = 0;
double count = 0;
for (Double a : list) {
sum += a;
count++;
}
System.out.println("Average is " + sum / count);
But when I use:
for (Object o : list) {
// How can I do the same with some code here?
}
How can I do the same with the Object
loop?
I hope you did understand my problem.
Ok, here is my method where I unserialize a file and where I want to get the values from...
public void deserialize(List<MySample>listan, String path) {
double sum=0;
int count=0;
try {File file = new File(path);ObjectInputStream in = new
ObjectInputStream(newFileInputStream(file));
listan = (ArrayList<MySample>) in.readObject();
for(Object obj: listan){
//here I need the code
}
System.out.println("Good work!");
in.close();
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}