如何向Array添加元素并移动索引?

我需要在Array中添加一个指定位置和值的元素。 例如,我有arrays

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

应用addPos(int 4, int 87)之后应该是

 int []a = {1, 2, 3, 4, 87, 5}; 

我知道这应该是Array索引的转换,但是看不到如何在代码中实现它。

这应该是诀窍:

 public static int[] addPos(int[] a, int pos, int num) { int[] result = new int[a.length]; for(int i = 0; i < pos; i++) result[i] = a[i]; result[pos] = num; for(int i = pos + 1; i < a.length; i++) result[i] = a[i - 1]; return result; } 

其中a是原始数组, pos是插入位置, num是要插入的数字。

您必须创建一个新数组,使用System.arraycopy复制前缀和后缀,并将该一个插槽设置为新值。

最简单的方法是使用ArrayList并使用add(int, T)方法。

 List list = new ArrayList(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); // Now, we will insert the number list.add(4, 87); 

我闻到了作业,所以可能不允许使用ArrayList(?)

而不是寻找“转移索引”的方法,也许只是构建一个新的数组:

 int[] b = new int[a.length +1]; 

然后

  1. 复制索引表单数组从零开始计数到插入位置

//编辑:当然复制值,而不是索引

这是一个准上线器:

 String[] prependedArray = new ArrayList() { { add("newElement"); addAll(Arrays.asList(originalArray)); } }.toArray(new String[0]); 

Jrad解决方案很好,但我不喜欢他不使用数组副本。 在内部,System.arraycopy()执行本机调用,因此您可以获得更快的结果。

 public static int[] addPos(int[] a, int index, int num) { int[] result = new int[a.length]; System.arraycopy(a, 0, result, 0, index); System.arraycopy(a, index, result, index + 1, a.length - index - 1); result[index] = num; return result; } 

看看公地 。 它使用arrayCopy(),但语法更好。 对于那些用逐元素代码回答的人:如果这不是家庭作业,那是微不足道的,而有趣的答案就是促进重用的答案。 对于那些提出名单的人:可能读者也知道这一点,应该提到性能问题。

除非我遗漏了什么,否则问题不在于增加数组大小。 在示例中,数组大小保持不变。 (就像一点点转移。)在这种情况下,没有理由创建新数组或复制它。 这应该是诀窍:

 static void addPos(int[] array, int pos, int value) { // initially set to value parameter so the first iteration, the value is replaced by it int prevValue = value; // Shift all elements to the right, starting at pos for (int i = pos; i < array.length; i++) { int tmp = prevValue; prevValue = array[i]; array[i] = tmp; } } 
 int []a = {1, 2, 3, 4, 5, 6}; addPos(a, 4, 87); // output: {1, 2, 3, 4, 87, 5} 

org.apache.commons.lang3.ArrayUtils#add(T[], int, T) #add org.apache.commons.lang3.ArrayUtils#add(T[], int, T)在最新公共语言lang3中不推荐使用,你可以使用org.apache.commons.lang3.ArrayUtils#insert(int, T[], T...) #insert org.apache.commons.lang3.ArrayUtils#insert(int, T[], T...)而不是。

不推荐使用此方法已被insert(int,T [],T …)取代,并且可能会在将来的版本中删除。 请注意,新输入数组的处理在新方法中有所不同:将X插入空数组会导致null而不是X.

示例代码:

  Assert.assertArrayEquals (org.apache.commons.lang3.ArrayUtils.insert (4, new int[]{1, 2, 3, 4, 5, 6}, 87), new int[]{1, 2, 3, 4, 87, 5, 6}); 

尝试这个

 public static int [] insertArry (int inputArray[], int index, int value){ for(int i=0; i< inputArray.length-1; i++) { if (i == index){ for (int j = inputArray.length-1; j >= index; j-- ){ inputArray[j]= inputArray[j-1]; } inputArray[index]=value; } } return inputArray; } 
 int[] b = new int[a.length +1]; System.arraycopy(a,0,b,0,4); //System.arraycopy(srcArray, srcPosition, destnArray, destnPosition, length) b[4]=87; System.arraycopy(a,4,b,5,2); 

b数组将创建为{1,2,3,4,87,5,6};

由于索引计算, System.arraycopy性能更高但很难处理。 如果您没有性能要求,最好坚持使用jrad answer或ArrayList

 public static int[] insert( int[] array, int elementToInsert, int index) { int[] result = new int[array.length + 1]; // copies first part of the array from the start up until the index System.arraycopy( array /* src */, 0 /* srcPos */, result /* dest */, 0 /* destPos */, index /* length */); // copies second part from the index up until the end shifting by 1 to the right System.arraycopy( array /* src */, index /* srcPos */, result /* dest */, index + 1 /* destPos */, array.length - index /* length */); result[index] = elementToInsert; return result; } 

并且JUnit4测试检查它是否按预期工作。

 @Test public void shouldInsertCorrectly() { Assert.assertArrayEquals( new int[]{1, 2, 3}, insert(new int[]{1, 3}, 2, 1)); Assert.assertArrayEquals( new int[]{1}, insert(new int[]{}, 1, 0)); Assert.assertArrayEquals( new int[]{1, 2, 3}, insert(new int[]{2, 3}, 1, 0)); Assert.assertArrayEquals( new int[]{1, 2, 3}, insert(new int[]{1, 2}, 3, 2)); } 
 public class HelloWorld{ public static void main(String[] args){ int[] LA = {1,2,4,5}; int k = 2; int item = 3; int j = LA.length; int[] LA_NEW = new int[LA.length+1]; while(j >k){ LA_NEW[j] = LA[j-1]; j = j-1; } LA_NEW[k] = item; for(int i = 0;i 

如果您更喜欢使用Apache Commons而不是重新发明轮子,那么目前的方法是:

 a = ArrayUtils.insert(4, a, 87); 

它曾经是ArrayUtils.add(…),但不久前已经弃用了。 更多信息: 1