I want to make a custom class MultipleResult
that can contain ArrayList
s of different types, but I'm not sure how to implement this. I want to encapsulate multiple ArrayList
into one object, but sometimes I'll use ArrayList<Float>
and other times ArrayList<Integer>
.
I have tried declaring the generic input as ArrayList<Object>
but this gives me an incompatible types
error when I do:
MultipleResult arrays = reduce(theInputValues,10);
ArrayList<Float> arrayA =arrays.getResultA();
where the method reduce
generates several ArrayList<Float>
and puts them into a MultipleResult
object. This is the MultipleResult
class:
import java.util.ArrayList;
public class MultipleResult {
private ArrayList<Object> resultA;
private ArrayList<Object> resultB;
private ArrayList<Object> resultC;
private ArrayList<Object> resultD;
public MultipleResult(ArrayList<Object> arrayA, ArrayList<Object> arrayB) {
resultA=arrayA;
resultB=arrayB;
}
public MultipleResult(ArrayList<Object> arrayA, ArrayList<Object> arrayB,
ArrayList<Object> arrayC, ArrayList<Object> arrayD) {
resultA=arrayA;
resultB=arrayB;
resultC=arrayC;
resultD=arrayD;
}
public ArrayList<Object> getResultA() {
return resultA;
}
public ArrayList<Object> getResultB() {
return resultB;
}
public ArrayList<Object> getResultC() {
return resultC;
}
public ArrayList<Object> getResultD() {
return resultD;
}
}
And here is the reduce
method:
private MultipleResult reduce(ArrayList<Float> theInput,Integer n){
ArrayList<Float> opens=new ArrayList<>();
ArrayList<Float> highs=new ArrayList<>();
ArrayList<Float> lows=new ArrayList<>();
ArrayList<Float> closes=new ArrayList<>();
Integer N = theInput.size();
for (int i=0;i<n;i++){
Integer nMin = Math.round((N/n)*i);
Integer nMax = Math.round((N/n)*(i+1))-1;
Float open=theInput.get(nMax);
Float high=theInput.get(nMin);
Float low=theInput.get(nMin);
Float close=theInput.get(nMin);
for(int j=nMin;j<=nMax;j++){
if (theInput.get(j)>high){
high=theInput.get(j);
}
if (theInput.get(j)<low){
low=theInput.get(j);
}
}
opens.add(i,open);
highs.add(i,high);
lows.add(i,low);
closes.add(i,close);
}
return new MultipleResult(opens,highs,lows,closes);
}