ERROR IN JAVA

i am trying to insert elements to arraylist using generics but i dont know why it is giving error can anyone sort this one out.

I think the problem is at line 19.

Are you sure about what you’re doing?

Whats wrong in line 19 trying to create an Arraylist of Integers.

Can you paste the whole error message?

i am a complete beginner so excuse me if i am wrong
but i think
maybe while passing the arguments you should try adding <I,nteger> along with the arr.
Pardon me if am wrong.

1 Like

because that’s what the error is suggesting

You are right but i am trying to pass that Integers using generics.


Because int value not convert in string

Since T is unknown during compilation ,so it is not possible to add int value in ArrayList of type T.
So you have to caste your value int type T,

You can use arr.add((T)i);
but it still give error because int is primitive and it is not possible to convert primitive value into Class T value so first here you have to convert int into Integer class object and than caste it into T.
so it becomes
arr.add((T)Integer.valueOf(i));

Now you can check the type of inserted value with
System.out.println(arr.get(i).getClass().getName());

it is java.lang.Integer

1 Like

tq bro finally its working