java - How do I put numbers in a generic list? -
so made generic list , accept strings if cast them (t). here's code:
package dz06; import java.util.arraylist; import java.util.list; public class exersise04<t> { public static void main(string[] args) { new exersise04(); } public exersise04(){ list<t> list = new arraylist<>(); list.add((t)"hello"); list.add((t)25); } }
this gives me error when want add int 25 if cast (t). if it's generic list shouldn't take whatever give it? please help, in advance
you cannot cast primitive int (t), try cast integer (t)
list.add((t)((integer)25));
(you can cast primitive int integer ((integer)25) because of automatic boxing.)
Comments
Post a Comment