简体   繁体   English

爪哇。 具有通用返回类型的策略设计模式

[英]java. Strategy design pattern with general return type

I want to make a strategy design pattern exemple in order to use it in my java application.我想制作一个策略设计模式示例,以便在我的 Java 应用程序中使用它。 So i have one function with tow possible value return types:所以我有一个具有两种可能值返回类型的函数:

int[][] getMatrix();
double[][] getMatrix();

I tried this:我试过这个:

//the strategy interface
public interface TutoInterface<T>{    
    T[][] getMatrix();   
}

and one of the tow classes that implements TutoInterface :以及实现TutoInterface的两个类TutoInterface

//strategy 1
public class Tuto implements TutoInterface<int> {

    @Override
    int[][] getMatrix() {
        //some code
    }
}

But the error indicated in the netbeans IDE is that int is not an object, so how can i do it?但是 netbeans IDE 中指出的错误是int不是对象,那么我该怎么做呢?

You can use hash structures, for example, Hash Table ( https://en.wikipedia.org/wiki/Hash_table )您可以使用哈希结构,例如哈希表 ( https://en.wikipedia.org/wiki/Hash_table )

The time of insertion and check is guaranteed to be O(1).插入和检查的时间保证为O(1)。 However, in the simplest form this structure is probabilistic.然而,在最简单的形式中,这种结构是概率性的。 Still works fine for the majority of applications.对于大多数应用程序仍然可以正常工作。

Did you try using hash table?您是否尝试使用哈希表? In most of cases complexity of inserting is O(1) and checking is O(1), but the data is not stored in order of insertion.在大多数情况下,插入的复杂度为 O(1),检查的复杂度为 O(1),但数据不是按插入顺序存储的。 You can combine this structure with a double-ended list of metadata, which you can iterate in order of insertion.您可以将此结构与元数据的双端列表相结合,您可以按插入顺序对其进行迭代。

If you simply save element in the hash table there can be primary clustering problem.如果您只是将元素保存在哈希表中,则可能存在主要聚类问题。 You can use hash-table with chaining.您可以使用带有链接的哈希表。

Search- O(1) Insert- O(1) Delete- O(n) Find- O(n)

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

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