Java InteliJ为数组的每个元素生成setter,而不是为整个数组生成setter
我有一节课Java InteliJ为数组的每个元素生成setter,而不是为整个数组生成setter,java,arrays,intellij-idea,Java,Arrays,Intellij Idea,我有一节课 Class HighScores{ private int[] scores = new int[10]; void setScores(int[] in){ this.scores = in; } } 但我想做的是能够将数组的每个元素设置为它自己的getter。InteliJ能生成getter吗?还是我必须像这样手写出来 Class HighScores{ private int[] scores = new int[
Class HighScores{
private int[] scores = new int[10];
void setScores(int[] in){
this.scores = in;
}
}
但我想做的是能够将数组的每个元素设置为它自己的getter。InteliJ能生成getter吗?还是我必须像这样手写出来
Class HighScores{
private int[] scores = new int[10];
void setScores(int inZero){
this.scores[0] = inZero;
}
void setScoresZero(int inZero){
this.scores[0] = inZero;
}
void setScoresOne(int inOne){
this.scores[1] = inOne;
}
}
谢谢。如果您使用,则可以获得非常类似的方法:
void setScoresToValue(int value) {
Arrays.fill(this.scores, value);
System.out.println(Arrays.toString(this.scores));
}
public static void main(String[] args) {
SomeClass sc = new SomeClass();
sc.setScoresToValue(255);
}
因此,如果需要,那么将数组设置为零调用
sc.setScoresToValue(0);
编辑:
如果您正在寻找一种在数组中的任何给定索引中设置值的方法,那么请考虑定义一种方法,在该方法中,可以传递索引和新值…< /P> 例子:
有两个参数,第一个索引和第二个值,怎么样?OP要求单独设置值,而不是一次设置所有值。@bradimus,thnxs注释。。。我认为现在是最新的答案。。。看一看,让我知道。。。。
void setScoresAtIndex(int index, int value) {
// you can either validate the index or let it throw the IndexOutOfBoundsException
this.scores[index] = value;
System.out.println(Arrays.toString(this.scores));
}