本文介绍了Java的泛型类型的原始N-D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的二维数组传递给过滤(中值滤波)滤波算法routine.The是array.Is有办法的类型相同,不论通过任何类型的数组中的一个通用的方式还是应该我重载不同阵列相同的同样的功能types.In第二种情况相同code将不得不重复不同的数据类型。

I have to pass a primitive 2d array to a filtering routine.The algorithm for filtering(median filter) is same irrespective of the type of the array.Is there a way to pass any type of array in a generic manner or should I overload the same same function with different array types.In the second case the same code will have to be repeated for different data types.

int[][] medianfilter(int[][] arr){ ... }
float[][] medianfilter(float[][] arr){ ... }

有没有一种方法,使上述code通用的,而不是重复code为中值滤波器在每一个每一个重载函数?

Is there a way to make the above code a generic one,instead of repeating the code for medianfilter in each an every overloaded function ?

推荐答案

通过在一个通用的方式,并保持它作为一个基本数组是作为对象的唯一方式。就个人而言,我只是超负荷,并认为这是使用原语的成本。

The only way to pass it in a generic manner and keep it as a primitive array is as an Object. Personally, I'd just overload it, and see it as a cost of using primitives.

要避免code重复的算法(如果它是一个很多code的),你可以生成一个名为像 DoubleAlgorithm 与抽象类像双getElement抽象方法(INT I,诠释J) handleResult(double结果),然后写非常小的子类此,每个原语类型

To avoid duplication of code in the algorithm (if it is a lot of code) you could produce an abstract class called something like DoubleAlgorithm with abstract methods like double getElement(int i, int j) and handleResult(double result) and then write very small subclasses of this, one for each primitive type.

让我用一个例子(假设算法添加数字)解释。

Let me explain with an example (suppose the algorithm was adding the numbers).

public int filter(int [][] values) {
   IntAlgorithm algo = new IntAlgorithm(values);
   algo.run();
   return algo.getResult();
}
public double filter(double [][] values) {
   DoubleAlgorithm algo = new DoubleAlgorithm(values);
   algo.run();
   return algo.getResult();
}

public class AbstractAlgorithm {
  public  run() {
     double sum = 0.0;
     for(int i=0; i<rows(); i++) {
       for(int j=0; j<columns(i); j++) {
          sum+=getElement(i, j);
       }
     }
     handleResult(sum);
  }
  protected abstract int rows();
  protected abstract int columns(int row);
  protected abstract double getElement(int i, int j);
  protected abstract void handleResult();
}

public class IntAlgorithm extends AbstractAlgorithm {
    int [][] values;
    int result;
    IntAlgorithm(int [][] values) {
       this.values= values;
    }
    public int rows() {
      return values.length;
    }
    public int columns(int row) {
      return values[row].length;
    }
    public double getElement(int i, int j) {
      return values[i][j];
    }
    public void handleResult(double result) {
      this.result = (int)result;
    }
    public int getResult() {
      return result;
    }
}

正如你所看到的,这是非常详细的,但如果你的算法是大它可能是值得的。希望这是显而易见如何扩展到你的算法。

As you can see, it is quite verbose, but if your algorithm was big it might be worth it. Hopefully it is obvious how to extend to your algorithm.

由于蒂洛所指出的,它是不是安全做所有的算法只处理整数/多头为双打,但对一些这将是不够好。如果它是不适合你,那么你需要去更加冗长,制定出你需要的数字的属性(如添加),并提取那些单独的接口。对于中值滤波,我只想用双打将正常工作期望,但我测试的边缘情况。

As Thilo has pointed out, it isn't safe to do all algorithms with just treating ints/longs as doubles, but for a number it will be good enough. If it isn't for you, then you need to go even more verbose, work out which properties of numbers you need (eg add) and extract those to a separate interface. For a median filter, I would expect just using doubles will work fine, but I'd test the edge cases.

这篇关于Java的泛型类型的原始N-D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:17