gpt4 book ai didi

java - 插入部分满数组

转载 作者:行者123 更新时间:2023-12-01 18:42:57 26 4
gpt4 key购买 nike

我们如何使用java将整数插入到部分满的数组中?该方法应该采用这些确切的参数:数组、数组中已有项目的计数、新插入元素和插入元素的索引。这是我到目前为止所拥有的。

  public static void insertIntoPartiallyFullArray(
int[] array,
int count,
int i,
int newThing
) {
if (count < array.length || i <= array.length - 1) {
array[i] = newThing;
count++;

}
}

最佳答案

试试这个:

public static int insertIntoPartiallyFullArray(
int[] array,
int count,
int item
) {
if (count >= array.length)
{ return array.length; } // the function breaks then

array[count] = insert;
return count++;
}

返回的int是数组的新计数。

该函数的作用是:

插入一个int item作为int[]数组中的下一个项目,其中已经有int count个项目

关于java - 插入部分满数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59886195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com