第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法想出移動數組的方法

無法想出移動數組的方法

江戶川亂折騰 2023-12-10 09:53:02
我陷入困境,無法想出一種方法來正確地將數組移動 __ 單位。我正在嘗試創(chuàng)建一個包含 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]+" ");    }  }}這是我到目前為止的代碼,對如何做到這一點有什么建議嗎?我嘗試創(chuàng)建一個單獨的 for 循環(huán),例如numArray [i-shiftNum] = numArray[i];但這樣做時,0-shiftNum 的索引將為負數,并且不起作用。這是問題的背景:創(chuàng)建一個程序,該程序將創(chuàng)建一個包含 30 個項目的數組。然后它將按照用戶選擇的數字旋轉陣列。
查看完整描述

3 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

為了移動數組中的數字,以下 for 循環(huán)可用于移動數組中的值。


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

    }

}

根據您的代碼,創(chuàng)建的數組將包含 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 循環(huán)?;旧?,您需要保存通過轉變覆蓋的元素。然后將那些保存的放回數組中。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條經驗 獲得超6個贊

這是為您提供的快速解決方案。請檢查以下代碼。


輸入 :


輸入移位/旋轉: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 關注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號