简体   繁体   English

Java 抽象基元数组加法

[英]Java abstract primitive array addition

When primitive arrays given,当给定原始 arrays 时,

int[] iArr = new int[]{0,1,2,3,4};
long[] lArr = new int[]{0,1,2,3,4};
double[] dArr = new int[]{0,1,2,3,4};

And Each provided by Supplier,并且每个由供应商提供,

class IarrSupplier {
  int[] supply();
}

class LarrSupplier {
  long[] supply();
}

class DarrSupplier {
  double[] supplyer();
}

How can I add them all in an abstraction way?如何以抽象的方式将它们全部添加? Such as

interface PSupplier<T> {
  T[] supply();
}

And providers are implements PSupplier<T>提供者是实现PSupplier<T>

class IarrSupplier implements PSupplier<int> {
  int[] supply();
}

class LarrSupplier implements PSupplier<long[]> {
  long[] supply();
}

class DarrSupplier implements PSupplier<double[]> {
  double[] supplyer();
}

I know primitive type generic is not supported in java.我知道 java 不支持原始类型泛型。 And so above will not work at all.所以上面根本不起作用。

But If it works, I hope to do,但如果它有效,我希望这样做,

// same or several type of PSuppliers will be provided.
PSupplier[] suppliers = polymorphsSuppliers();

long[] totalSumArray = [my_fixed_length];
for(PSupplier supplier : suppliers){
  for(int i = 0 ; i < totalSumArray.length ; i++){
    totalSumArray[i] += supplier.supply()[i];
  }
}

What would possible/better design to do this?有什么可能/更好的设计来做到这一点?

In short,简而言之,

  • Multiple int[] or long[] (or more summable primitive types data) will be given.将给出多个 int[] 或 long[](或更多可求和的原始类型数据)。
  • I want to get grand total all of them.我想得到所有这些。
  • I want to make a long[] that accumulate all of each same index value of given arrays.我想做一个 long[] 来累积给定 arrays 的所有相同索引值。

I don't know what you want to do exactly, but I can see several problems about your code.我不知道您到底想做什么,但是我可以看到有关您的代码的几个问题。 I guess you want to iterate over an unidimensional array, so, in the interface PSupplier it's not neccesary declare the method that returns an array T[] , so the fixed code would be:我猜你想遍历一个一维数组,所以,在接口PSupplier中,不需要声明返回数组T[]的方法,所以固定代码是:

interface PSupplier<T> { T supply(); }

Now the next classes which implement from PSupplier interface, their generic type must be an array of the data type you want, for example:现在从 PSupplier 接口实现的下一个类,它们的泛型类型必须是您想要的数据类型的数组,例如:

class IarrSupplier implements PSupplier<int[]> {
    @Override
    public int[] supply() {
        return new int[]{1,2,3,4,5,6,7};
    }
}

Remember that the generics accept only Objects, and, unless in java, an array is an object, it doesn't matter if its type is primitive,so, for that reason, you can type PSupplier<int[]> and will be correct.请记住,generics 只接受对象,并且,除非在 java 中,否则数组是 object,它的类型是否正确并不重要,PSupplier 类型是否正确,所以, [int] . Or you can use PSupplier<Integer[]> and will be correct too, because Integer is a wrapper class for int data type.或者您可以使用PSupplier<Integer[]>并且也是正确的,因为 Integer 是 int 数据类型的包装 class 。 If you don't know about wrapper classes in java, I recommend read about that: Wrapper classes in java如果您不了解 java 中的包装类,我建议您阅读: java 中的包装类

For the method polymorphsSuppliers() must return an array of PSupplier , for example:对于方法polymorphsSuppliers()必须返回一个PSupplier数组,例如:

** here, the polymorphism is working **

  PSupplier[] polymorphsSuppliers(){
    return new PSupplier[]
    {
        new IarrSupplier(),
        new LarrSupplier(),
        new DarrSupplier(),
        ......
    };
}

About your code in the for loops, I don't understand what you want to do exactly.关于你在 for 循环中的代码,我不明白你到底想做什么。 But I hope the examples I typed work for you.但我希望我输入的示例对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM