java - Insert object into List wildcard extending interface -


so have interface

public interface identity {  abstract void setkey(string key); abstract string getkey(); } 

that want use insert sort function

public static void insertidentity(identity identity, arraylist<? extends identity> list) {     int = 0;     identity id;     while (i < list.size()) {         if ((id = list.get(i)).getkey().compareto(identity.getkey()) < 0) {             i++;         }else{break;}     }     list.add(i, identity); } 

but cannot add identity arraylist - ? extends identity

i wondering if there way around this?

i know java has other ways of doing task function work.

you make method generic:

public static <t extends identity> void insertidentity(t identity, arraylist<t> list) {     //... 

you may need make other methods generic well, if use approach.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -