简体   繁体   English

在Java中将数组的所有元素移回一个

[英]Shifting all elements of an array back one in Java

I am writing a code that will shift all elements of an array back one, and move the last element to the front of the array. 我正在编写代码,将数组的所有元素向后移动,并将最后一个元素移至数组的前面。

I pretty much want my program to do this: 我非常希望我的程序做到这一点:

int[] array = new int[] {1, 2, 3};
// do something such that array will become {3, 1, 2}

My code is as follows: 我的代码如下:

int[] array = new int[3];


for(int i = 0; i < array.length; i++)
{
  array[0] = 1;
  array[1] = 2;
  array[2] = 3;
  int last = array[array.length-1];
  array[i] = array[i+1];
  array[0] = last;

  System.out.println(array[i]);
}

I thought that adding 1 to "i" for the array, and storing the last element of the array in "last" and assigning it to array[0] would do the trick, but I'm just getting {3, 3} in the output with "java.lang.ArrayIndexOutOfBoundsException: 3." 我以为将“ 1”添加到数组的“ i”,然后将数组的最后一个元素存储在“ last”中,然后将其分配给array [0]可以解决问题,但是我只是在{3,3}中输出为“ java.lang.ArrayIndexOutOfBoundsException:3”。

If what you really want is to rotate it (sounds a bit like it). 如果您真正想要的是旋转它(听起来有点像它)。 Use Collections.rotate() : 使用Collections.rotate()

Collections.rotate(Arrays.asList(array), 1);

Where 1 is the number of steps (distance) 其中1是步数(距离)

If you don't want to convert the array to a list you could do the rotate manually with simple methods like this (for example) 如果您不想将数组转换为列表,则可以使用这样的简单方法手动进行旋转(例如)

Going left: 向左走:

void rotateArrayLeftByOne(int array[])
{
    // get last index of array
    int lastIndex = array.length - 1; 
    // save first element
    int oldFirst = array[0]; 

    // copy the elements from  right to left
    for (int i = 0; i < lastIndex; i++) 
        array[i] = array[i + 1];

    // put the first element last
    array[lastIndex] = oldFirst;
}

and going right: 往右走:

void rotateArrayRightByOne(int array[])
{
    // get last index of array
    int lastIndex = array.length - 1; 
    // save last element
    int oldLast = array[lastIndex]; 

    // copy the elements from  left to right
    for (int i = lastIndex; i != 0; i--) 
        array[i] = array[i - 1];

    // put the last element first
    array[0] = oldLast;
}

You could try with something like this: 您可以尝试使用以下方法:

int[] originalArray = ...
int[] shiftedArray = new int[originalArray.length];

System.arraycopy(originalArray, 0, shiftedArray, 1, originalArray.length - 1);
shiftedArray[0] = originalArray[originalArray.length - 1];

EDIT As suggested, only one array is actually needed 编辑建议,实际上只需要一个数组

int[] originalArray = ...
int lastItem = originalArray[originalArray.length - 1];

System.arraycopy(originalArray, 0, originalArray, 1, originalArray.length - 1);
originalArray [0] = lastItem;

You are trying access an index that does not exist in your array. 您正在尝试访问数组中不存在的索引。 Your array's size is 3 therefore 2 is the last index. 您的数组大小为3,因此2是最后一个索引。 Also, you are initializing and printing in the for loop. 另外,您正在for循环中进行初始化和打印。 You should do those outside of the loop. 您应该在循环之外执行这些操作。

int[] array = new int[3];

array[0] = 1;
array[1] = 2;
array[2] = 3;

for(int i = 0, previous_value = array[array.length]; i < array.length; i++)
{
  int prev = array[i];
  array[i] = previous_value;
  previous_value = prev;
}

for(int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}

With O(1) space and O(n) time. 具有O(1)空间和O(n)时间。

int[] array = new int[3];

for(int i = 0; i < array.length; i++) array[i] = i;

int temp = array[array.length-1];

for(int i = array.length-1; i > 0; i--) array[i] = array[i-1];

array[0] = temp;

First, the problem is that actually your are triying to access a element that doesn't exists. 首先,问题是您实际上正在尝试访问不存在的元素。 Whe you run this code 您在运行此代码

array[i] = array[i+1];

and the "i" variable is 2, doing i + 1 will lead you to array[3] which doesn't exists. 并且“ i”变量为2,执行i +1将导致您找到不存在的array [3]。

Also, working directly with array can lead to memory consumption problems if you are triying to sort, you should better using collections, check http://docs.oracle.com/javase/tutorial/collections/algorithms/ for a good tutorial on how to handle this kind of operations 此外,如果要进行排序,直接使用数组可能会导致内存消耗问题,因此最好使用集合,请访问http://docs.oracle.com/javase/tutorial/collections/algorithms/,以获得有关如何进行操作的良好教程处理这类操作

you can iterate from end to begin. 您可以从头到尾进行迭代。 You begin with creating an empty slot in the end, then start to fill the empty slot with the previous value. 您首先要在最后创建一个空插槽,然后开始用先前的值填充空插槽。 There is no need of creating a temporary array. 无需创建临时数组。 Everything is done in place . 一切都准备in place

int n=3;
int[] array = new int[n];
int last = array[n-1];    //copy last element into buffer thus creating an empty slot
for(int i=n-1;i>0;i--)    //iterate from end to begin
{
   array[i] = array[i-1]; //push previous element into current location(an empty slot)
}
array[0] = last;          //finally put the last element into initial slot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM