gpt4 book ai didi

java - 如果在某个索引位置插入一个元素,则将数组元素移动到下一个位置

转载 作者:行者123 更新时间:2023-11-30 03:19:43 24 4
gpt4 key购买 nike

下面是我的输入数组

int[] myarray= {1,2,3,4,5,6,7};

int inputNo=10; // value to be inserted

int index=3; // position to be inserted

然后输出应该,

{1,2,3,10,4,5,6,7}

最佳答案

int[] insertedArray = new int[myarray.length+1];
System.arrayCopy( myarray, 0, insertedArray, 0, index );
System.arrayCopy( myarray, index, insertedArray, index+1, myarray.length-index);
insertedArray[index] = inputNo;

也许,取决于上下文......

myarray = insertedArray

关于java - 如果在某个索引位置插入一个元素,则将数组元素移动到下一个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31648253/

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