亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

采用整型數組、整型索引和整型值的方法

采用整型數組、整型索引和整型值的方法

catspeake 2022-09-14 10:39:35
因此,對于像我這樣的初學者來說,這個問題有點高級,這就是為什么我希望你能忽略我的錯誤。我們需要創建一個采用數組的方法,一個我想要放在數組中的值以及數組中的索引值(空格)。因為我的老師對她想要的東西并不具體,所以她沒有具體說明數組必須是空的還是滿的,這是本練習中最讓我困惑的地方。靜態方法必須命名為:addAtIndex( int [] a, int value, int index),然后執行以下操作:如果數組中的最后一個空格為0或空,并且我想在其中一個已經占用的空間中放置一個新的整數值,則需要將每個元素向右移動并保持該空間自由,以便能夠執行此操作。如果最后一個空間已經被整數值占用,我需要“調整”數組的大小并騰出更多空間,以便我可以繼續輸入更多的整數值。我知道你不能調整數組的大小,更不能保持所有元素的完整性,所以我需要創建一個更大的數組來做到這一點。我用Java編程還不到兩個月,所以我發現這很困難。我已經能夠創建一個空數組,并且我已經在數組中使用給定的參數輸入了不同的數值。所有這些都是在公共靜態空白主而不是方法中完成的。我似乎無法在方法中執行此操作,特別是我不知道如何將數組中的所有內容都向右移動。import java.util.Arrays;import java.util.Scanner;public class Array{  public static void main (String args []){     Scanner input = new Scanner(System.in);     int [] array   = new int[10];    for (int x = 0; x <= 9; x++){     int index = input.nextInt();     int value    = (int)(Math.random()*50);      //move elements below insertion point.    for (int i = array.length-1; i > index; i--){        array[i] = array[i-1];       }       //insert new value        array[index] = value;      }      System.out.println(Arrays.toString(array));   }}當然,這與老師希望我做的事情相去甚遠。我在這里所做的只是創建一個數組,在我選擇的索引中輸入隨機整數并將其打印出來。我需要一些幫助。
查看完整描述

1 回答

?
阿波羅的戰車

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

這就是我以最簡約的方式做到這一點的方式。

你可能需要調整,因為我沒有花那么多時間在它上面。

但是,它應該給你一個很大的推動力。


請記住,數組不能具有空值。所以我把一個“空”值設為零。

關注評論!int[]


static int[] addAtIndex(

        final int[] array,

        final int value,

        final int index) {

    if (array == null || array.length == 0) {

        // We cannot do anything.

        // Simply return to the caller

        return array;

    }


    // This is the array reference holder

    int[] localArray = array;


    // Get the last element of the array

    final int last = localArray[localArray.length - 1];


    // Is the last element 0? Remember an int array cannot contain nulls

    if (last == 0) {

        if (array[index] != 0) {

            // We need to shift everything to the right

            // to give space to the new element

            shiftRight(localArray, index);

        }

    } else {

        // Create a bigger array of length = actualLength + 1

        // and assign it to the reference holder

        localArray = new int[array.length + 1];


        // Copy the array elements to the new array, leaving a space

        // for the new element

        copyArrayAndLeaveSpace(index, array, localArray);

    }


    // Assign the new value

    localArray[index] = value;


    // Return the modified array reference

    return localArray;

}


static void shiftRight(

        final int[] array,

        final int index) {

    System.arraycopy(array, index, array, index + 1, array.length - index - 1);

}


static void copyArrayAndLeaveSpace(

        final int index,

        final int[] array,

        final int[] newArray) {

    System.arraycopy(array, 0, newArray, 0, index);

    System.arraycopy(array, index, newArray, index + 1, array.length - index);

}

用法


final int[] nums = {1, 2, 3, 4, 5, 6, 8, 9, 0};

final int[] result = addAtIndex(nums, 7, 4);

輸出:[1, 2, 3, 4, 7, 5, 6, 8, 9]


final int[] nums = {1, 2, 3, 4, 5, 6, 8, 9, 1};

final int[] result = addAtIndex(nums, 7, 4);

輸出:[1, 2, 3, 4, 7, 5, 6, 8, 9, 1]


查看完整回答
反對 回復 2022-09-14
  • 1 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

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

公眾號

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