myheadhurts
myheadhurts

Reputation: 203

Left hand must be a variable

If i do this it works

elements.getElement(j).getValues().getArray()[i]=(elements.getElement(j).getValues().getVariable(i)-minimum)/(maximum-minimum);

But if I do this it tells me that he left side should be a variable

elements.getElement(j).getValues().getVariable(i)=(elements.getElement(j).getValues().getVariable(i)-minimum)/(maximum-minimum);

The two functions are

double[] getArray(){
    return array;
}

double getVariable(int position){
    return array[position];
}

I believe that what is in the front does not matter and it is working properly and the mistake must lie somewhere at the end. And how come when I assign let's say like this ( 1st row) it works , which means it is a variable but when I try to assign something to it like(2nd row) it does not work.How dumb am I ? What am I missing ?

double max=elements.getElement(j).getValues().getVariable(i);
elements.getElement(j).getValues().getVariable(i)=0.0;

Upvotes: 4

Views: 277

Answers (7)

MadConan
MadConan

Reputation: 3767

Let's say there these methods

public int[] getArray(){
    return array;
}

public int getValue(int i){
   return array[i];
}

Your question implies you expect these both to work

getArray()[0] = 2;
getValue(0) = 3;

The first one works because if you "unwind" the call, you get

int[] arrayFromMethod = getArray();
arrayFromMethod[0] = 2;

The second one won't work because you are getting an int value, not an array. And attempting to unwind it is really the equivalent of

// pretend array[0] is already 42
 42 = 3;  // Nope

It isn't the same as

int x = array[0];
x = 3;

because you inline the call when you say getValue(i) = 3, which isn't allocating a new variable. And even if it did, the value would be gone as soon as the next statement is reached, so it doesn't have any use.

Upvotes: 0

user35443
user35443

Reputation: 6413

getVariable(i)

Is definitely different from

getArray()[i]

The first one returns an array, which may be indexed by [] and then used as a valid l-value. The latter returns a value (r-value), that can only be used on the left side of an assignemnt.

In simple words, you can not assign a value to a value, but you can assign a value to a memory location specified by an array and an index.

0 = 5 // this just won't work
array[0] = 5 // this will, because array[0] is a valid l-value

You may, however, create a method that would serve as a setter.

void setVariable(int position, double value) {
    array[position] = value;
}

It would be used like this

elements.getElement(j).getValues().setVariable(i, value);

Upvotes: 7

gurghet
gurghet

Reputation: 7707

You can think about it like this.

Functions have this form countLetters("hello") and they visually send their return value to the left;

int count = countLetters("hello");
      <---------------------+

What you’re doing is visually bumping on the parenthesis.

getValue(1) = 5;
  <------+X<--+

Hope this helps you remember.

Upvotes: 1

Kevin Workman
Kevin Workman

Reputation: 42174

The getVariable() function returns a value. You can use it any place you can use a primitive. So instead of this:

int x = 7;

You can do this:

int x = getVariable(i);

However, note that you could not do this:

3 = 7;

Since 3 is a value, not a variable. Your code is trying to do something like that, so you're getting an error.

The getArray() function works because it returns an array, and you then use the [] index operator on it, which you can use as a variable.

getArray()[i] = 5;

Upvotes: 1

rgettman
rgettman

Reputation: 178263

When you access an array with the bracket characters, [i], that is an array access expression, and Java allows that expression on the left side of an assignment operator.

However, with the call to getVariable(i), that is a method, and the things returned by methods are always values, which are not allowed on the left side of an assignment operator. Where would the value to be assigned go? We only have a value, not a variable.

If you want to avoid exposing the array, you can create and use a setVariable method to write a value in a list at a specified index.

void setVariable(int position, double value){
    array[position] = value;
}


elements.getElement(j).getValues().setVariable(i, 
    elements.getElement(j).getValues().getVariable(i)-minimum)/(maximum-minimum);

Upvotes: 2

Antoniossss
Antoniossss

Reputation: 32517

Functions are not substitutions, so when you are doing

elements.getElement(j).getValues().getVariable(i)

you think that this is equivalent to this

elements.getElement(j).getValues().getArray()[i]

but it is not. Why? It is quite simple;

Return value of first call will be a VALUE of array[i]. In the second case, return of the function is array itself, so you can assign elements to it and read from it as well.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726669

The error is self-explanatory: you cannot assign to a result returned from a method call.

It may appear that your first code snippet makes an assignment to the result returned from a method, but that appearance is misleading: your first code snippet applies the index operator [] to the array result of calling a method, and only then assigns to the result produced by the [] operator.

Assigning to what's returned by [] is allowed, hence your first code snippet compiles and runs correctly. The second snippet, however, applies [] inside method implementation, and returns a double. At this point the returned double has no connection to the array from which it came, making it illegal to assign.

In order to fix this, add another method, and perform the assignment inside:

void setVariable(int position, double newValue){
    array[position] = newValue;
}
...
elements.getElement(j)
    .getValues()
    .setVariable(i
    , (elements.getElement(j).getValues().getVariable(i)-minimum)/(maximum-minimum)
    );

Upvotes: 2

Related Questions