猿问

更改具有 N 个范围步长的数组中的数据?

以 N 个范围步长更改数组中的数据,例如,每 2 个步长。


int 数据 = 新的 int[8];


result:


    [0],[0], [0],[0], [0],[0], [0],[0];

预期:前两项应更改为 1,接下来的两项将保持为 0,依此类推...


[1],[1] ,[0],[0], [1],[1], [0],[0];

我知道诀窍


如果(位置 % 2 == 0)


用于更改每 2 个项目,但它只更改第一个项目。知道如何解决吗?


  int bars =2;

    int beats = 4;

    int[] pattern = new int[bars * beats];

    for (int i = 0; i < pattern.length; i++) {

        if(i % bars == 0 ){

            pattern[i] = 0;

        }else{

            pattern[i] = 1;

        }

    }

不是最优雅的解决方案,但可以使用 static int[] 数据;


public static void main(String[] args) {


    int bars = 7;

    int beats = 2;


    data = new int[bars * beats];

    int minVal;


     if(bars > beats){

          minVal = Math.min(bars, beats);

     }else{

          minVal = Math.max(bars, beats);

     }

    step(minVal, 1);


    for (int i = 0; i < data.length; i++) {

        if(i % minVal == 0){

            System.out.print("|"+ data[i]);

        }else{

            System.out.print(data[i]);

        }

    }

}



public static void step(int interval, int value) {

    for (int index = 0; index < data.length; index += interval) {

        for (int stepIndex = index; stepIndex < index + interval; stepIndex++) {

            if (stepIndex > data.length - 1) {

                return;

            }

            data[stepIndex] = value;

        }

        index += interval;

    }

}




   static int[] data;


    public static void main(String[] args) {


        int bars = 7;

        int beats = 2;


        data = new int[bars * beats];

        int minVal;


         if(bars > beats){

              minVal = Math.min(bars, beats);

         }else{

              minVal = Math.max(bars, beats);

         }

        step(minVal, 1);


        for (int i = 0; i < data.length; i++) {

            if(i % minVal == 0){

                System.out.print("|"+ data[i]);

            }else{

                System.out.print(data[i]);

            }

        }

    }


UYOU
浏览 117回答 2
2回答

慕田峪9158850

尝试这个int bars = 2;int beats = 4;int[] pattern = new int[bars * beats];for (int i = 0; i < pattern.length; i++) {&nbsp; &nbsp;if(i % beats < bars ){&nbsp; &nbsp; &nbsp; pattern[i] = 1;&nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; pattern[i] = 0;&nbsp; &nbsp;}}

茅侃侃

这是实现此目标的众多方法之一。我们遍历数组,按定义的间隔递增,例如您希望为 2。我们创建另一个 for 循环,从当前索引开始到当前索引 + 间隔结束,这将允许我们将值(在您的情况下为 1)分配给这些索引。我们还检查我们正在循环的当前索引是否大于数据长度 - 1,以确保对于非偶数数组大小,没有数组索引超出绑定。public class ChangeArrayNSteps {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ChangeArrayNSteps cans = new ChangeArrayNSteps(8);&nbsp; &nbsp; &nbsp; &nbsp; cans.step(2, 1);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Data: " + Arrays.toString(cans.data));&nbsp; &nbsp; }&nbsp; &nbsp; private final int[] data;&nbsp; &nbsp; public ChangeArrayNSteps(int size) {&nbsp; &nbsp; &nbsp; &nbsp; this.data = new int[size];&nbsp; &nbsp; }&nbsp; &nbsp; public void step(int interval, int value) {&nbsp; &nbsp; &nbsp; &nbsp; for (int index = 0; index < data.length; index += interval) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int stepIndex = index; stepIndex < index + interval; stepIndex++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (stepIndex > data.length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[stepIndex] = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index += interval;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出: Data: [1, 1, 0, 0, 1, 1, 0, 0]
随时随地看视频慕课网APP

相关分类

Java
我要回答