gpt4 book ai didi

java - 如何将引用数组传递给setter?

转载 作者:行者123 更新时间:2023-12-02 04:39:05 25 4
gpt4 key购买 nike

编写一个名为 setTo5 的方法,该方法传递对整数数组的引用,并将该数组的内容设置为全 5。这是我到目前为止所拥有的,它在 public static void main(...); 之后的第二行给了我一个错误。如何成为更好的二传手?

public class Set5 {
private static int n = 8;
static int[] boop = new int[n];

public static void main(String[] args){
int[] roop = new int[n];
roop.setTo5(5);

}

public void setTo5(int poop){
for(int i = 0; i<n ; i++){
poop = boop[i];

}
}

}

最佳答案

尝试这样的事情:

public class Set5 {
private static int n = 8;
static int[] boop = new int[n];

public static void main(String[] args){
int[] roop = new int[n];

//Create instance of Set5 class to call setter
Set5 set5reference = new Set5();

set5reference.setTo5(roop);

//All the values in roop will now be 5 as set by setter.

}

//change this method to accept array reference like this
public void setTo5(int[] poop){
for(int i = 0; i<n ; i++){
poop[i] = 5;
}
}
}

关于java - 如何将引用数组传递给setter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30405981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com