Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "Variable Expected" error on list.get(0) = 2

Here is a simple java code that I wrote:

    List<Integer> list = new ArrayList<>();
    list.add(0);
    list.get(0) = 2; // Trying to set first list element's value to 2

The third line gives me an error, it says:

java: unexpected type
required: variable
found:    value

Does anyone know why I am getting this type of error?

like image 721
Jason Avatar asked May 29 '26 11:05

Jason


1 Answers

set() method is used to set the element in the particular index

List<Integer> list = new ArrayList<>();
list.add(0);
list.set(0, 2);
like image 158
Aayush Saini Avatar answered May 31 '26 07:05

Aayush Saini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!