java - Generic return type - unable to compile -


i'm reading oracle generics tutorial, , i'm having problem getting following code work:

import java.util.*; public class genericreturn {     public static void main(string[] args){             integer index = 1;             arraylist<integer> al = new arraylist<>();             al.add(10);             al.add(20);             al.add(30);             number s = choose(index, al);             system.out.println(s);     }     static <t, u, s> s choose(t a1, u a2) { return a2.get(a1); } } 

it won't compile errors are:

javac genericreturn.java  genericreturn.java:12: error: cannot find symbol     static <t, u> t choose(t a1, u a2) { return a2.get(a1); }                                                   ^ symbol:   method get(t) location: variable a2 of type u t,u type-variables: t extends object declared in method <t,u>choose(t,u) u extends object declared in method <t,u>choose(t,u) 1 error 

can please me out here?

someone's want mark duplicate, please don't - generics hard enough without ploughing through similar question doesn't quite cover need know!

change :

static <t, u, s> s choose(t a1, u a2) { return a2.get(a1); } 

to :

static <u extends list<s>, s> s choose(integer a1, u a2) { return a2.get(a1); } 

or more simplified version :

static <s> s choose(integer a1, list<s> a2) { return a2.get(a1); } 

that being said, collection classes in java generic there no need write such wrapper method in first place.


Comments

Popular posts from this blog

how to do line continuation in perl debugger for entering raw multi-line text (EOT)? -

javascript - Create websocket without connecting -

android - Linear layout children not scrolling -