【发布时间】:2017-11-24 00:07:15
【问题描述】:
这是我的 getter 和 setter:
private int[] test;
public int[] getTest() {
return Arrays.copyOf(test, test.length);
}
public void setTest(int[] test) {
this.test= Arrays.copyOf(test, test.length);
}
这是我手动将值传递给 setter 方法的代码
Sample sample = new Sample();
sample.setTest(new int[]{0,1,2,3});
我想要的是这样的:
for (int i = 0; i < 3; i++) {
//code here for passing the value to the setter
}
这对我有用,有没有办法使用 for 循环传递这些值?另外我想知道如何将数组作为一个整体传入?
【问题讨论】:
-
不清楚你的意思。你能举个例子说明你想写什么吗?
-
for (int i = 0; i > 3; i++)我想你的意思是i < 3 -
您可以将值推送到循环中的数组中并通过 setter 或创建另一个方法来将单个值推送到您的私有属性。
-
不过,好问题 ;-)
-
@GhostCat 谢谢 :) 希望这个问题对其他人也有帮助:)
标签: java arrays for-loop getter-setter