public class example { public ArrayList<Integer> ToFill = new ArrayList<>(); public void Alter(int Value , int Position) { ToFill.get(Position) = Value ; // this line has an error } }
For some reason this code gives compilation Error ,could anyone explain why?
Advertisement
Answer
ToFill.get(Position)
returns a value where the left-hand side of the assignment must be a variable. Instead, use set(index, element)
as follows:
ToFill.set(Position, Value);
However, what you are doing is only valid if you are using arrays, for example:
Integer[] array = ... array[Position] = Value;
As a side note, always use Java naming convention:
toFill
instead ofToFill
alter
instead ofAlter
position
instead ofPosition
.value
instead ofValue
.