英文:
Error at using Assignment operator in java
问题
我还遇到了以下代码行的错误:“赋值的左侧必须是一个变量”
outPutArray.charAt(i)=inputArray[i]
如何解决这个问题?我尝试加入括号但没有用
(outPutArray.charAt(i))=inputArray[i]
英文:
I also the error "Left Hand Side of an Assignment must be a Variable" for following line
outPutArray.charAt(i)=inputArray[i]
how to resolve this? I have tried putting braces like but of no use
(outPutArray.charAt(i))=inputArray[i]
答案1
得分: 1
你正在尝试改变一个String对象。这是不可能的,String被设计为不可变的。你需要使用改变后内容创建一个新的String,可以使用char数组,或者其他能实际达到你最终目标的方式(即:给定一个输入String,返回一个其中一个字符改变的输出String)。
解决了这个问题后,你不能对函数的返回值进行赋值。你无法得到一个可更改的引用,进而改变某个变量的值。你得到的是一个值。关于这个问题,这个很棒的链接或许值得一读:https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1。
英文:
You are trying to mutate a String object. This is not possible, String is supposed to be immutable. You have to create a new String with the changed content, use a char array instead, or something else that allows you to actually achieve what you are trying to do in the end (which is: given an input String, return an output String where one character is changed).
With that out of the way, you can't do an assignment on the return value of a function. You don't get a reference back that you can change and then some variable gets changed. You get a value back. The great https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1 question is maybe a good read.
答案2
得分: 0
非常愚蠢,但格式应为:
inputArray[i] = outPutArray.charAt(i);
而不是
outPutArray.charAt(i) = inputArray[i];
将函数放在左侧意味着你正在将常量整数赋值给整数数组元素。由于整数是常量,它将失败。这就是为什么我们将变量(在你的情况下是inputArray[i])放在左边,将函数放在右边(outPutArray.charAt(i))。希望你明白!
英文:
Very silly, but the format should be:
inputArray[i] = outPutArray.charAt(i);
not
outPutArray.charAt(i) = inputArray[i]
putting the function on the left hand side means you are assigning a constant integer to a integer array element. As the integer is constant, it will fail. This is why we put our variables(inputArray[i] in your case) on the left hand side and our functions on the right hand(outPutArray.charAt(i)). Hope you understand!
答案3
得分: 0
.charAt(i)函数返回位置i处的字符。它不会返回该索引处字符的引用。
更多详细信息请参阅此处
英文:
.charAt(i) function returns the character at position i. It does not return the reference to the character at that index.
Look at this for more details
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论