4

I want to make a custom class MultipleResult that can contain ArrayLists 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);
}
Rafael Sanchez
  • 355
  • 6
  • 18

4 Answers4

4

As suggested in comment by @Kaostias you can make MultipleResult generic as follows

public class MultipleResult<T> {

    private ArrayList<T> resultA;
    private ArrayList<T> resultB;
    private ArrayList<T> resultC;
    private ArrayList<T> resultD;

    public MultipleResult(ArrayList<T> arrayA, ArrayList<T> arrayB) {
        resultA=arrayA;
        resultB=arrayB;
    }

    public MultipleResult(ArrayList<T> arrayA, ArrayList<T> arrayB,
                          ArrayList<T> arrayC, ArrayList<T> arrayD) {
        resultA=arrayA;
        resultB=arrayB;
        resultC=arrayC;
        resultD=arrayD;
    }

    public ArrayList<T> getResultA() {
        return resultA;
    }

    public ArrayList<T> getResultB() {
        return resultB;
    }

    public ArrayList<T> getResultC() {
        return resultC;
    }

    public ArrayList<T> getResultD() {
        return resultD;
    }
}

and use it as follows

private MultipleResult<Float> 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<Float>(opens,highs,lows,closes);
}
Ilya
  • 1,932
  • 11
  • 18
0

im not sure but i think in your case generic type is useful. for example :

ArrayList<T> list = new ArrayList<T>();

for more information use link bellow:

https://docs.oracle.com/javase/tutorial/java/generics/types.html

How to create a generic array?

Community
  • 1
  • 1
pouyan
  • 3,192
  • 2
  • 21
  • 38
0

You can do that using generics. For example:

  public class MultipleResult<T> {
   ...
  }

and then:

 MultipleResult<Integer> multipleResult = new MultipleResult<>();
0

You can make MultipleResult itself parametric, using that parametric type to type its contained arrays.

public class MultipleResult<T extends Number> {
    private List<T> resultA = new ArrayList<>();

    public MultipleResult(List<T> arrayA, ...) {
        this.resultA = arrayA;
    }
}
John Coker
  • 124
  • 4