【问题标题】:Method that takes an int array, int index and int value采用 int 数组、int 索引和 int 值的方法
【发布时间】:2019-03-31 20:56:31
【问题描述】:

所以,对于像我这样的初学者来说,这个问题有点高级,这就是为什么我希望你能忽略我的错误。

我们需要创建一个方法,该方法接受一个数组、一个我想放在数组中的值以及数组中的索引值(空格)。

因为我的老师没有具体说明她想要什么,所以她没有具体说明数组是空的还是满的,这是这个练习中最让我困惑的地方。

静态方法必须命名:

addAtIndex( int [] a, int value, int index) 

并执行以下操作:

    1234563能够做到这一点。
  • 如果最后一个空间已被整数值占用,我需要“调整”数组的大小并腾出更多空间,以便继续输入更多整数值。我知道你不能调整数组的大小,更不用说保持所有元素完整,所以我需要创建一个更大的数组来做到这一点。

我用 Java 编程还不到两个月,所以我觉得这很困难。

我已经能够创建一个空数组,并且我在给定参数的数组中输入了不同的数值。所有这些都是在 public static void main 中完成的,而不是在方法中。我似乎无法在方法中做到这一点,尤其是我不知道如何将数组中的所有内容向右移动。

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

这当然与老师要我做的事情相去甚远。我在这里所做的只是创建一个数组,在我选择的索引中输入随机整数并将其打印出来。我需要一些帮助。

【问题讨论】:

  • 更新了示例和一些重构。
  • 顺便说一句,如有任何疑问,请询问。

标签: java arrays


【解决方案1】:

我会以最简约的方式做到这一点。
您可能需要调整,因为我没有花太多时间在上面。
但是,它应该会给您带来很大的提升。

记住int[] 数组不能有空值。所以我将“空”值设为零。
跟随 cmets!

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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2010-10-02
    相关资源
    最近更新 更多