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

Popular posts from this blog

javascript - Why Selenium can't find an element that is graphically visible -

java - How to compare two classes -

mysql - Gateway Timeout Error on Insert 70000 record using Hibernate in Java -