InteliJ为数组的每个元素生成一个setter,而不是整个数组

时间:2016-07-26 16:29:27

标签: java arrays intellij-idea

我有一个班级

Class HighScores{
    private int[] scores = new int[10];

    void setScores(int[] in){
        this.scores = in;
    }
}

但我想能够做的是能够将数组的每个元素设置为它自己的getter。 InteliJ可以产生吸气剂吗?或者我是否必须像这样用手写出来?

  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;
         }
    }

感谢。

1 个答案:

答案 0 :(得分:-1)

如果您使用Arrays.fills(),那么您可以采用非常类似的方法:

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);

编辑:

如果您正在寻找一种方法来设置数组中任何给定索引的值,那么请考虑定义一个可以传递索引和新值的方法...

实施例

   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));
    }