无法想出移动数组的方法

我陷入困境,无法想出一种方法来正确地将数组移动 __ 单位。我正在尝试创建一个包含 30 个项目(数字 1-30)的数组,然后可以将其按用户输入的数字向右移动。这意味着数组中的前几个数字将采用数组末尾的索引,其余数字将向左移动。(例如,如果 shift = 3,数字 1,2,3 将采用索引 27,28,29,其余数字 4-30 将左移,使索引 0 =4,索引 1=5,索引 2 =6....


import java.util.*;


class Main {

  public static void main(String[] args) {

    Scanner input = new Scanner (System.in);


    System.out.println("\nEnter the shift/rotation:");

    int shiftNum = input.nextInt();


    int [] numArray = new int [30];


    for(int i = 0; i < 30; i++){

        numArray [i] = i+1;

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

    }


  }

}

这是我到目前为止的代码,对如何做到这一点有什么建议吗?我尝试创建一个单独的 for 循环,例如


numArray [i-shiftNum] = numArray[i];

但这样做时,0-shiftNum 的索引将为负数,并且不起作用。这是问题的背景:


创建一个程序,该程序将创建一个包含 30 个项目的数组。然后它将按照用户选择的数字旋转阵列。


江户川乱折腾
浏览 94回答 3
3回答

慕尼黑的夜晚无繁华

为了移动数组中的数字,以下 for 循环可用于移动数组中的值。// prerequisite: array is already filled with valuesfor(int i = 0; i < numArray.length; i++) {&nbsp; &nbsp; arr[i] += shiftNum;&nbsp; &nbsp; if (numArray[i] > 30) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; numArray[i] -= 30;&nbsp; &nbsp; } else if (numArray[i] <= 0) {&nbsp; &nbsp; &nbsp; &nbsp; numArray[i] += 30;&nbsp; &nbsp; }}根据您的代码,创建的数组将包含 1 - 30 之间的值,包括 1 和 30。如果您希望代码包含 0 - 29 之间的值,请将 numArray[i] > 30 更改为 numArray[i] >= 30 并将 numArray[i] <= 0 更改为 numArray[i] < 0。

湖上湖

使用 Java 的便捷方法。大多数人仍然想编写 for 循环。基本上,您需要保存通过转变覆盖的元素。然后将那些保存的放回数组中。System.arraycopy 很好,因为它可以处理数组中移动元素的一些令人讨厌的部分。void shift(int shiftBy, int... array) {&nbsp; &nbsp; int[] holdInts = Arrays.copyOf(array, shiftBy);&nbsp; &nbsp; System.arraycopy(array, shiftBy, array, 0, array.length - shiftBy);&nbsp; &nbsp; System.arraycopy(holdInts, 0, array, array.length - shiftBy, holdInts.length);}

杨魅力

这是为您提供的快速解决方案。请检查以下代码。输入 :输入移位/旋转:4输出 :旋转给定数组 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]旋转后 [27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 , 21, 22, 23, 24, 25, 26]&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; RotationDemo rd = new RotationDemo();&nbsp; &nbsp; int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};&nbsp; &nbsp; int k = 0;&nbsp; &nbsp; Scanner scan = new Scanner (System.in);&nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("\nEnter the shift/rotation:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int shiftNum = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(shiftNum < 30) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k = shiftNum;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Rotate given array " + Arrays.toString(input));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int[] rotatedArray = rd.rotateRight(input, input.length, k);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("After Rotate&nbsp; " +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.toString(rotatedArray));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Shift number should be less than 30");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} catch(Exception ex){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scan.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; public int[] rotateRight(int[] input, int length, int numOfRotations) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < numOfRotations; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int temp = input[length - 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = length - 1; j > 0; j--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input[j] = input[j - 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input[0] = temp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return input;&nbsp; &nbsp; &nbsp; }希望这个例子能起作用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java