Java 移动数组中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24131465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shifting elements in array
提问by
I am trying to shift elements in an array. I tried from the book but it does not seem to work. The user types in how many to shift the array by in the main class, and then it gets sent into a shifter class into a shift method. Starting from array position [1].
我正在尝试移动数组中的元素。我从书中尝试过,但似乎不起作用。用户在主类中输入要移动数组的数量,然后将其发送到 shifter 类中的 shift 方法。从数组位置 [1] 开始。
This is what I have:
这就是我所拥有的:
// Pos is the users entered for how many positions to shift
// data is the array of elements 1-length of data
// temp is place holder
public void shift(int pos)
{
while(pos > 0)
{
int temp = data[data.length];
for(int i = pos; i == data.length; i++ )
{
data[i+1] = data[i];
}
data[data.length] = temp;
pos--;
}
}
采纳答案by Am_I_Helpful
The reason why you are getting ArrayIndexOutOfBoundsExceptionis that your data array has a size of data.length
(counting from 1), but you have tried to access the data[data.length]
element in the last iteration of the loop which is data.length+1
element of the array which doesn't exist and is out of bound of array, because of array index starting from 0 in Java.
您收到ArrayIndexOutOfBoundsException的原因是您的数据数组的大小为data.length
(从 1 开始计数),但是您尝试访问data[data.length]
循环的最后一次迭代中的data.length+1
元素,该元素是不存在的数组元素,并且是超出数组范围,因为在 Java 中数组索引从 0 开始。
Correct Code :-
正确代码:-
// within your while loop
int temp=data[data.length-1];
for(int i=data.length-1;i>=1;i--)
{
data[i] = data[i-1];
}
// decrease the position now
data[0]=temp;
Kindly check the below link to know more about this Exception :-
请查看以下链接以了解有关此异常的更多信息:-
回答by zgc7009
Put it in a while loop with some sort of break command, in this example "quit"
使用某种中断命令将其放入 while 循环中,在本例中为“退出”
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
boolean run = true;
while(run){
System.out.println("What would you like to shift?");
String input = br.readLine();
if(input.equals("quit")) break;
else{
int pos = Integer.parseInt(input);
int temp = data[pos];
for(int i = pos; i<data.length-1; i++) {
data[i] = data[i+1];
}
}
}
Of course you will need to do error checking on your input to make sure it is a valid position in the array. I am, however, posting this from a droid and coding on a phone is a pain.
当然,您需要对输入进行错误检查,以确保它是数组中的有效位置。然而,我是从机器人上发布的,在手机上编码是一种痛苦。
This code also does a bit more than you are asking, but it gives an idea behind the logic of the while loop. Also, I just read your edit and I'm not sure I understand what exactly is going on, but again hope this may help. Would have put it as a comment but obviously it's a bit lengthy for that.
这段代码的功能也比您要求的要多一些,但它提供了 while 循环逻辑背后的想法。另外,我刚刚阅读了您的编辑,我不确定我是否理解到底发生了什么,但再次希望这可能会有所帮助。本来可以把它作为评论,但显然它有点冗长。
回答by gp.
if you want to shift right and circularly, from 1 to end by pos
positions this is what you can do:
如果你想向右和循环移动,从 1 到结束pos
位置,这是你可以做的:
public void shift(int pos) {
while (pos-- > 0) {//shift 1 right for pos number of times
//notice the data.length-1 for the last item. data.length as index would be out of bound.
int tmp = data[data.length - 1];
//start from the last and keep shifting the left one to current.
for (int i = data.length - 1; i > 1; i--) {
data[i] = data[i - 1];
}
//since it's a circular shift, last one (before shifting) will shift to 1 index.
data[1] = tmp;
}
}
回答by user3475136
int temp=data[data.length-1];
for(int i=data.length-1;i>=1;i--)
{
data[i+1] = data[i];
}
data[0]=temp;
回答by Ege Kuzubasioglu
Probably the easiest solution:
可能是最简单的解决方案:
private static void shiftArray(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
for (int p : array)
System.out.print(p + " ");
}
}
回答by lukaspp
Sorry for the late answer, but I think the easiest way is to se module like this:
抱歉回答晚了,但我认为最简单的方法是像这样设置模块:
int[] original = { 1, 2, 3, 4, 5, 6 };
int[] reordered = new int[original.length];
int shift = 1;
for(int i=0; i<original.length;i++)
reordered[i] = original[(shift+i)%original.length];