Skip to content
Advertisement

Unexpected type required variable found value

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 of ToFill
  • alter instead of Alter
  • position instead of Position.
  • value instead of Value.
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement