包含任意对象的

时间:2016-05-18 15:21:18

标签: java android arraylist

我想创建一个可以包含MultipleResult个不同类型的自定义类ArrayList,但我不确定如何实现它。我想将多个ArrayList封装到一个对象中,但有时我会使用ArrayList<Float>,有时会使用ArrayList<Integer>。 我已经尝试将通用输入声明为ArrayList<Object>但是这样做时会出现incompatible types错误:

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

方法reduce生成多个ArrayList<Float>并将其放入MultipleResult对象。这是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;
    }
}

这是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 个答案:

答案 0 :(得分:4)

根据@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);
}

答案 1 :(得分:0)

我不确定,但我认为在你的情况下通用类型是有用的。例如:

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

有关更多信息,请使用以下链接:

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

How to create a generic array?

答案 2 :(得分:0)

你可以使用泛型来做到这一点。例如:

  public class MultipleResult<T> {
   ...
  }

然后:

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

答案 3 :(得分:0)

您可以使用参数类型对MultipleResult本身进行参数化,以键入其包含的数组。

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

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