Java 函数正在更新其输入变量
我目前正在做一个研究健身功能的实验室。问题是我正在制作一个 SmallChange() 函数,它执行它所说的操作,令人烦恼的是,它似乎更新了您给它的变量,而实际上它不应该更新。
这是完整课程的副本: https://gist.github.com/1710367
第 38 行是问题线。
下面是该函数。当我将 solution
作为输入时,它会用它所做的小更改来更新 solution
,但我不知道如何或为什么。
有人知道我哪里出错了吗?它开始伤害我的大脑。
// Edits either the angle or velocity of our solution
// by a small amount (about 1% of the diffrence between max and min)
public static Double[] SmallChange(Double[] sol) {
// Pick a 0 or 1
switch (r.nextInt(2)) {
case 1: // Changing the angle
// The angle change amount
Double angle = (angleLimits[1] - angleLimits[0]) * 0.01;
// Make the change, either + or -
if (r.nextInt(2) == 0) {
sol[0] += angle;
} else {
sol[0] -= angle;
}
// Check it's within the limits
if (sol[0] > angleLimits[1]) {
sol[0] = angleLimits[1];
} else if (sol[0] < angleLimits[0]) {
sol[0] = angleLimits[1];
}
break;
case 0: // Changing the velocity
// The velocity change amount
Double velocity = (velocityLimits[1] - velocityLimits[0]) * 0.01;
// Make the change, either + or -
if (r.nextInt(2) == 0) {
sol[1] += velocity;
} else {
sol[1] -= velocity;
}
// Check it's within the limits
if (sol[1] > velocityLimits[1]) {
sol[1] = velocityLimits[1];
} else if (sol[1] < velocityLimits[0]) {
sol[1] = velocityLimits[1];
}
break;
}
return sol;
}
I'm doing a lab at the moment working on fitness functions. The problem is I'm making a SmallChange() function that does what it says, annoyingly it seems to update the variable you give it, when it shouldn't.
Here's a copy of the full class: https://gist.github.com/1710367
Line 38 is the problem line.
And below is the function. When I give it solution
as an input it updates solution
with the small change it makes, but I can't figure out how or why.
Anyone know where I'm going wrong? It's starting to hurt my brain.
// Edits either the angle or velocity of our solution
// by a small amount (about 1% of the diffrence between max and min)
public static Double[] SmallChange(Double[] sol) {
// Pick a 0 or 1
switch (r.nextInt(2)) {
case 1: // Changing the angle
// The angle change amount
Double angle = (angleLimits[1] - angleLimits[0]) * 0.01;
// Make the change, either + or -
if (r.nextInt(2) == 0) {
sol[0] += angle;
} else {
sol[0] -= angle;
}
// Check it's within the limits
if (sol[0] > angleLimits[1]) {
sol[0] = angleLimits[1];
} else if (sol[0] < angleLimits[0]) {
sol[0] = angleLimits[1];
}
break;
case 0: // Changing the velocity
// The velocity change amount
Double velocity = (velocityLimits[1] - velocityLimits[0]) * 0.01;
// Make the change, either + or -
if (r.nextInt(2) == 0) {
sol[1] += velocity;
} else {
sol[1] -= velocity;
}
// Check it's within the limits
if (sol[1] > velocityLimits[1]) {
sol[1] = velocityLimits[1];
} else if (sol[1] < velocityLimits[0]) {
sol[1] = velocityLimits[1];
}
break;
}
return sol;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Java 中,一切都按值传递 - 但该值始终是基元或引用。
因此任何数组变量的值都是对数组对象的引用。当您使用该变量作为参数时,该值(引用)最终将作为参数的初始值。它仍然引用与调用者变量相同的数组对象 - 因此对数组所做的任何更改都将被调用者看到。
所以为了澄清一下,你在这里的说法是不正确的:
它根本没有改变变量的值:调用代码中的变量仍然具有与之前相同的值,对同一数组的引用。只是该方法正在更改数组的内容。
这就像将您的地址抄在一张纸上,然后将其交给某人:他们无法更改您居住的地方,但可以更改您前门的颜色。
现在,要解决您的问题...
如果您想克隆数组,则必须明确执行此操作。例如:
您可以重新分配给
sol
,但我个人不会。请注意,您可能还想使用
double[]
而不是Double[]
。In Java, everything is passed by value - but that value is always either a primitive, or a reference.
So the value of any array variable is a reference to an array object. When you use that variable as an argument, the value (the reference) ends up as the initial value of the parameter. It's still referring to the same array object as the caller's variable - so any changes made to the array will be seen by the caller.
So just to clarify, your statement here is incorrect:
It hasn't changed the value of the variable at all: that variable in the calling code still has the same value it did before, a reference to the same array. It's just that the method is changing the contents of the array.
It's like copying down your address on a piece of paper, then giving that to someone: they can't change where you live, but they can change the colour of your front door.
Now, to fix your problem...
If you want to clone the array, you'll have to do so explicitly. For example:
You could reassign to
sol
, but personally I wouldn't.Note that you might also want to use a
double[]
instead ofDouble[]
.