Java方法更改传递给它的变量
我试图在我的程序中实现加密,但在方法上遇到了一些困难。我有一个未加密的数组,它应该保持未加密状态,还有另一个我想加密的数组。但是,当我调用该方法时,它会加密两个数组。
import java.util.Base64;
public class test {
public static void main(String[] args) {
String[] arr = {"1", "2"};
String[] arrEncrypted = encryption(arr);
System.out.println(arr[0]);
System.out.println(arrEncrypted[0]);
}
public static String[] encryption(String[] data) {
for (int i = 0; i < data.length; i++) { // get each element of data
String encodedString = Base64.getEncoder().encodeToString(data[i].getBytes()); // encryption
data[i] = encodedString; // reassign
}
return data;
}
}
我找不到问题,因为根据我的理解,我返回加密数据并将其分配给另一个数组,但由于某种原因,我传递的数组也发生了变化。
这是输出
MQ== //has to be 1
MQ== //correct
提前感谢您的帮助!
I was trying to implement encryption in my program and ran into some difficulties with methods. I have a unencrypted array, which should stay unencrypted and another array which I would like to encrypt. However, when I call the method it encrypts both arrays.
import java.util.Base64;
public class test {
public static void main(String[] args) {
String[] arr = {"1", "2"};
String[] arrEncrypted = encryption(arr);
System.out.println(arr[0]);
System.out.println(arrEncrypted[0]);
}
public static String[] encryption(String[] data) {
for (int i = 0; i < data.length; i++) { // get each element of data
String encodedString = Base64.getEncoder().encodeToString(data[i].getBytes()); // encryption
data[i] = encodedString; // reassign
}
return data;
}
}
I cannot find out an issue because in my understanding I return encrypted data and assign it to another array, but for some reason an array which I passed changes too.
Here is the output
MQ== //has to be 1
MQ== //correct
Thanks in advance for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用
encryption(arr)
时,您将传递该对象的引用的值,这意味着您最终会直接改变它。如果您没有返回data
并将encryption
声明为void
返回,您的输入数组仍将是“加密的”。如果您只想返回一个新数组而不修改输入,您可以初始化一个与输入长度相同的新的空
String[]
,并在 for 循环中为其分配元素。例如,
请参阅 Java 是否“按引用传递”或“按值传递”?,以全面解释传入
data
参数时幕后发生的情况。When you invoke
encryption(arr)
you are passing the value of the reference for that object through, which means you end up directly mutating it. If you did not returndata
and declaredencryption
asvoid
return, your input array would still be "encrypted."If you'd like to solely return a new array without modifying the input, you can initialise a new, empty
String[]
of the same length as the input, and assign elements to it in your for loop.For example,
Please see Is Java "pass-by-reference" or "pass-by-value"? for a thorough explanation of what's going on under the hood when you pass in your
data
argument.三件事:
你不能“传递变量”,你只能传递变量的值。
变量“arr”不是数组,它的值是对数组的引用。
因此,变量“data”获取“arr”值的副本,它是对同一数组的引用。
您应该为输出分配一个新数组,例如
Three things:
You can't "pass a variable", you can only pass the value of a variable.
The variable "arr" is not the array, it has a value which is a reference to the array.
And therefore the variable "data" gets a copy of the value of "arr", which is a reference to the same array.
You should allocate a new array for the output, something like