Java的:在移动数组项 [英] Java: moving items in array
问题描述
我在寻找一个阵列内移动的东西。
欲能够在移动的那些在当前位置到右侧的给定阵列内移动的最后一个项目,以一个点。我希望它从第一点移动到第二等而不替换为当前存在的项目。
EX)
A,B,C,D,E
说我想要移动到3 - 那么这将成为
A,B,C,E,D
目前,我有以下几点:
公共静态无效moveLastup(字符串[]的东西,INT位置)
{
字符串Y =东西[stuff.length-1]; 对于(INT X = stuff.length-1; X>位置; x--)
列表[X] =列表[X-1]; 东西[位置] = Y;
}
编辑:对不起,我不认为我是不够清楚。我希望能够做的就是给这个方法是什么,我应该能够最后一块任意移动。
的for(int POS = 0; POS< stuff.length; POS ++)
{
moveLastup(单,POS)
showList(名单);
}
现在,当我执行此,它只是需要在接下来的列表中的最后一个项目中的for循环
前)
E,A,B,C,DE,D,A,B,CE,D,C,B,A
我想它显示
E,A,B,C,DA,E,B,C,DA,B,E,C,D
下面是一个更有效,更简洁的解决方案,依托本地实施 System.arraycopy
:
公共静态无效moveLastup(字符串[]编曲,诠释POS){
字符串最后= ARR [arr.length-1]; //复制子阵开始在pos到POS + 1
System.arraycopy(ARR,POS,编曲,POS + 1,arr.length - POS - 1); ARR [POS] =最后;
}
和一些测试code:
公共静态无效的主要(字串[] args){
的String [] =考{一,二,三,四有,十二五}; //将五个一指数2
moveLastup(测试,2); // [一,二,五,三,四]
的System.out.println(Arrays.toString(试验));
}
关于你的编辑:您正在使用和修改原始数组。如果你想在为从头再来每个 moveLastup
你需要在副本上工作。这段代码打印你想要什么:
的String [] =列表{A,B,C,D,E};对于(INT POS = 0; POS< List.length的数字; POS ++){
的String [] = tmpCopy list.clone();
moveLastup(tmpCopy,POS)
showList(tmpCopy);
}
输出:
[
电子
,A,B,C ,D]
[一,
电子
,B,C,D]
[A,B,
电子
,C,D]
[A,B,C,
电子
D]。
[A,B,C,D,
电子
]
块引用>I'm looking to move things around within an Array.
I want to be able to move the last item within the given Array to a spot while moving those in the current location to the right. I want it to move from the first spot to the second, etc. without replacing the item that is currently there.
ex)
a,b,c,d,e
Say I want to move to "3" - it would then become
a,b,c,e,d
I currently have the following:
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; }
edit: sorry I don't think I was clear enough. What I want to be able to do is given this method, I should be able to move the last piece anywhere.
for (int pos = 0; pos < stuff.length; pos++) { moveLastup(list,pos); showList(list); }
Now when I execute this, it simply takes the last item in the next list in the for loop ex)
e,a,b,c,d e,d,a,b,c e,d,c,b,a
i would like it to show
e,a,b,c,d a,e,b,c,d a,b,e,c,d
解决方案Here's a more efficient and concise solution, relying on the natively implemented
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; }
And some test code:
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)); }
Regarding your edit: You're working with and modifying the original array. If you want to "start over" in each
moveLastup
you need to work on a copy. This snippet prints what you want:String[] list = { "a", "b", "c", "d", "e" }; for (int pos = 0; pos < list.length; pos++) { String[] tmpCopy = list.clone(); moveLastup(tmpCopy, pos); showList(tmpCopy); }
Output:
[
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
]
这篇关于Java的:在移动数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!