简体   繁体   中英

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. 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 :

//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?

You can use hash structures, for example, Hash Table ( https://en.wikipedia.org/wiki/Hash_table )

The time of insertion and check is guaranteed to be 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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