Java:在数组中移动项目

我想在数组中移动东西。

我希望能够将给定arrays中的最后一个项目移动到一个点,同时将当前位置中的那些项目移动到右侧。 我希望它从第一个点移动到第二个点等,而不替换当前存在的项目。

EX)

a,b,c,d,e 

说我想转移到“3” – 它会成为

 a,b,c,e,d 

我目前有以下内容:

 public static void moveLastup(String[] stuff, int position) { String y = stuff[stuff.length-1]; for (int x = stuff.length-1; x > position; x--) list[x] = list[x-1]; stuff[position] = y; } 

编辑:对不起,我觉得我不够清楚。 我希望能够做到的是这种方法,我应该可以将最后一块移动到任何地方。

 for (int pos = 0; pos < stuff.length; pos++) { moveLastup(list,pos); showList(list); } 

现在当我执行它时,它只需要在for循环ex中的下一个列表中的最后一项

 e,a,b,c,d e,d,a,b,c e,d,c,b,a 

我想要展示

 e,a,b,c,d a,e,b,c,d a,b,e,c,d 

这是一个更高效,更简洁的解决方案,依赖于本机实现的System.arraycopy

 public static void moveLastup(String[] arr, int pos) { String last = arr[arr.length-1]; // Copy sub-array starting at pos to pos+1 System.arraycopy(arr, pos, arr, pos + 1, arr.length - pos - 1); arr[pos] = last; } 

还有一些测试代码:

 public static void main(String[] args) { String[] test = { "one", "two", "three", "four", "five" }; // Move "five" to index 2 moveLastup(test, 2); // [one, two, five, three, four] System.out.println(Arrays.toString(test)); } 

关于你的编辑:你正在使用和修改原始数组。 如果你想在每个moveLastup “重新开始”,你需要处理一个副本。 此代码段打印出您想要的内容:

 String[] list = { "a", "b", "c", "d", "e" }; for (int pos = 0; pos < list.length; pos++) { String[] tmpCopy = list.clone(); moveLastup(tmpCopy, pos); showList(tmpCopy); } 

输出:

[ e , a, b, c, d]
[a, e , b, c, d]
[a, b, e , c, d]
[a, b, c, e , d]
[a, b, c, d, e ]

我知道问题是关于arrays – 不想开始讨论性能“与”问题和“为什么我使用纯数组” – 但对于那些使用List我认为这可能是有用的。

java.util.Collections.rotate方法。 是的,这个名字很奇怪,通过查看javadoc的一部分:

请注意,此方法可以有用地应用于子列表,以移动列表中的一个或多个元素,同时保留其余元素的顺序。 例如,以下习语将索引j处的元素向前移动到位置k(必须大于或等于j):

     Collections.rotate(list.subList(j,k + 1), -  1);

为了使这个具体,假设列表包括[a,b,c,d,e]。 要将索引1(b)处的元素向前移动两个位置,请执行以下调用:

     Collections.rotate(l.subList(1,4), -  1);

结果列表是[a,c,d,b,e]。

要向前移动多个元素,请增加旋转距离的绝对值。 要向后移动元素,请使用正移位距离。

 public void moveElement(List list, int a, int b) { // forward or backward int direction = a > b ? 1 : -1; // always from minor to major to subList int minor = a < b ? a : b; int major = b > a ? b : a; Collections.rotate(list.subList(minor, major + 1), direction); } 

首先,在您的代码中

 for (int x = stuff.length-1; x > pos; x--) 

如果pos没有定义,我建议将它改为位置。 第二,将“列表”改为“东西”。

修改后的工作代码:

 public static void moveLastup(String[] stuff, int position) { String y = stuff[stuff.length-1]; for (int x = stuff.length-1; x > position; x--) stuff[x] = stuff[x-1]; stuff[position] = y; } 

你没有使用List而不是String[]任何特殊原因? 它将使这些类型的操作更容易。 使用ArrayList ,您只需要:

 list.add(3, list.remove(list.size() - 1)); 

如果使用LinkedList甚至更短:

 list.add(3, list.removeLast()); 

这是一个基于你的更完整的例子:

 LinkedList list = new LinkedList(); list.addAll(Arrays.asList("a", "b", "c", "d", "e")); list.add(3, list.removeLast()); System.out.println(list); // prints "[a, b, c, e, d]" 

基于System.arraycopy另一个简短快速的解决方案:

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

数组内容从索引“insert”“向右推”。

这是一些演示代码:

 int[] array = {1,2,3,4,5}; int insert = 2; int last = array[array.length-1]; System.arraycopy(array, insert, array, insert+1, array.length-insert-1); array[insert] = last; for (int value:array) System.out.println(value); 

你可以这样做:

 char arr1[]=new arr1[5]; // arr1 contains a,b,c,d,e in order char arr2[]=new arr2[5]; for(int i=0;i