为了账号安全,请及时绑定邮箱和手机立即绑定

无法想出移动数组的方法

无法想出移动数组的方法

江户川乱折腾 2023-12-10 09:53:02
我陷入困境,无法想出一种方法来正确地将数组移动 __ 单位。我正在尝试创建一个包含 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 个项目的数组。然后它将按照用户选择的数字旋转阵列。
查看完整描述

3 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

为了移动数组中的数字,以下 for 循环可用于移动数组中的值。


// prerequisite: array is already filled with values

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

    arr[i] += shiftNum;


    if (numArray[i] > 30) { 

        numArray[i] -= 30;

    } else if (numArray[i] <= 0) {

        numArray[i] += 30;

    }

}

根据您的代码,创建的数组将包含 1 - 30 之间的值,包括 1 和 30。如果您希望代码包含 0 - 29 之间的值,请将 numArray[i] > 30 更改为 numArray[i] >= 30 并将 numArray[i] <= 0 更改为 numArray[i] < 0。


查看完整回答
反对 回复 2023-12-10
?
湖上湖

TA贡献2003条经验 获得超2个赞

使用 Java 的便捷方法。大多数人仍然想编写 for 循环。基本上,您需要保存通过转变覆盖的元素。然后将那些保存的放回数组中。System.arraycopy 很好,因为它可以处理数组中移动元素的一些令人讨厌的部分。


void shift(int shiftBy, int... array) {

    int[] holdInts = Arrays.copyOf(array, shiftBy);

    System.arraycopy(array, shiftBy, array, 0, array.length - shiftBy);

    System.arraycopy(holdInts, 0, array, array.length - shiftBy, holdInts.length);

}


查看完整回答
反对 回复 2023-12-10
?
杨魅力

TA贡献1811条经验 获得超5个赞

这是为您提供的快速解决方案。请检查以下代码。


输入 :


输入移位/旋转: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]


    public static void main(String[] args) {

    RotationDemo rd = new RotationDemo();

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

    int k = 0;

    Scanner scan = new Scanner (System.in);

    try{

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

         int shiftNum = scan.nextInt();

         if(shiftNum < 30) {

             k = shiftNum;

             System.out.println("Rotate given array " + Arrays.toString(input));

             int[] rotatedArray = rd.rotateRight(input, input.length, k);

             System.out.println("After Rotate  " + 

                  Arrays.toString(rotatedArray));

         } else {

            System.out.println("Shift number should be less than 30");

         }

         } catch(Exception ex){

         } finally {

            scan.close();

        }

      }

      public int[] rotateRight(int[] input, int length, int numOfRotations) {

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

          int temp = input[length - 1];

          for (int j = length - 1; j > 0; j--) {

            input[j] = input[j - 1];

          }

          input[0] = temp;

        }

        return input;

      }

希望这个例子能起作用。


查看完整回答
反对 回复 2023-12-10
  • 3 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信