ArrayList containing arbitrary objects

I want to create a custom class MultipleResultthat can contain ArrayListdifferent types, but I'm not sure how to implement this. I want to encapsulate several ArrayListin one object, but sometimes I will use ArrayList<Float>other times ArrayList<Integer>. I tried to declare general input as ArrayList<Object>, but this gives me an error incompatible typeswhen I do this:

MultipleResult arrays = reduce(theInputValues,10);
ArrayList<Float> arrayA =arrays.getResultA();

where the method reducegenerates several ArrayList<Float>and places them in an object MultipleResult. This is the class MultipleResult:

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 method reduce:

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);
}
+4
source share
4 answers

@Kaostias, MultipleResult generic

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;
    }
}

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);
}
+4

im , , . :

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

:

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

?

0

You can do this with generics. For instance:

  public class MultipleResult<T> {
   ...
  }

and then:

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

You can make the MultipleResult parameter parametric by using this parametric type to enter its contained arrays.

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

    public MultipleResult(List<T> arrayA, ...) {
        this.resultA = arrayA;
    }
}
0
source

All Articles