Java,移动数组中的元素
我有一个 Java 对象数组,我试图将一个元素拉到顶部,并将其余元素向下移动一个。
假设我有一个大小为 10 的数组,并且我正在尝试提取第五个元素。第五个元素进入位置 0
,并且从 0 到 5 的所有元素都将向下移动 1。
该算法无法正确移动元素:
Object temp = pool[position];
for (int i = 0; i < position; i++) {
array[i+1] = array[i];
}
array[0] = temp;
我该如何正确地做到这一点?
I have an array of objects in Java, and I am trying to pull one element to the top and shift the rest down by one.
Assume I have an array of size 10, and I am trying to pull the fifth element. The fifth element goes into position 0
and all elements from 0 to 5 will be shifted down by one.
This algorithm does not properly shift the elements:
Object temp = pool[position];
for (int i = 0; i < position; i++) {
array[i+1] = array[i];
}
array[0] = temp;
How do I do it correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
编写一个Java程序,创建一个包含20个整数的数组,然后实现将数组右移两个元素的过程。
Write a Java program to create an array of 20 integers, and then implement the process of shifting the array to right for two elements.
从逻辑上讲,它不起作用,您应该反转循环:
或者您可以使用
Logically it does not work and you should reverse your loop:
Alternatively you can use
假设您的数组是 {10,20,30,40,50,60,70,80,90,100}
您的循环执行的操作是:
迭代 1: array[1] = array[0]; {10,10,30,40,50,60,70,80,90,100}
迭代 2: array[2] = array[1]; {10,10,10,40,50,60,70,80,90,100}
你应该做的是
Assuming your array is {10,20,30,40,50,60,70,80,90,100}
What your loop does is:
Iteration 1: array[1] = array[0]; {10,10,30,40,50,60,70,80,90,100}
Iteration 2: array[2] = array[1]; {10,10,10,40,50,60,70,80,90,100}
What you should be doing is
您只需使用
Collections.rotate(List list, int distance)
使用
Arrays.asList(array)
转换为List
更多信息位于:https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#rotate(java.util.List,%20int)
You can just use
Collections.rotate(List<?> list, int distance)
Use
Arrays.asList(array)
to convert toList
more info at: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#rotate(java.util.List,%20int)
只是为了完整性:自 Java 8 以来的流解决方案。
我想我在您的情况下坚持使用 System.arraycopy() 。但最好的长期解决方案可能是将所有内容都转换为不可变集合(Guava,Vavr),只要这些集合是短暂的。
Just for completeness: Stream solution since Java 8.
I think I sticked with the
System.arraycopy()
in your situtation. But the best long-term solution might be to convert everything to Immutable Collections (Guava, Vavr), as long as those collections are short-lived.您可以使用这样的模块使此功能更加通用,而不是移动一个位置。
Instead of shifting by one position you can make this function more general using module like this.
正如您所发现的,以这种方式操作数组很容易出错。更好的选择可能是使用 LinkedList在你的情况下。对于链表和所有 Java 集合,数组管理是在内部处理的,因此您不必担心元素的移动。使用 LinkedList,您只需调用
remove
,然后调用addLast
即可完成。Manipulating arrays in this way is error prone, as you've discovered. A better option may be to use a LinkedList in your situation. With a linked list, and all Java collections, array management is handled internally so you don't have to worry about moving elements around. With a LinkedList you just call
remove
and thenaddLast
and the you're done.试试这个:
在这里查看它的工作原理:http://www.ideone.com/5JfAg
Try this:
Look here to see it working: http://www.ideone.com/5JfAg
使用数组复制
k 次移位 k=1 或 k=3 等
代码的通用解决方案
Using array Copy
Generic solution for k times shift k=1 or k=3 etc
code
在循环的第一次迭代中,您将覆盖
array[1]
中的值。您应该以相反的顺序查看索引。In the first iteration of your loop, you overwrite the value in
array[1]
. You should go through the indicies in the reverse order.如果您将数组数据作为 Java 列表,则还有另一种变体
只是分享我为此遇到的另一个选项,但我认为答案来自 @ Murat Mustafin 是列出清单的最佳方式
Another variation if you have the array data as a Java-List
Just sharing another option I ran across for this, but I think the answer from @Murat Mustafin is the way to go with a list
对大小为 n 的数组进行左旋转操作会将数组的每个元素单位向左移动,检查一下!!!!!!!
A left rotation operation on an array of size n shifts each of the array's elements unit to the left, check this out!!!!!!
您可以使用以下代码进行移位而不是旋转:
用于将大小为
n
的数组向左移位d
个元素的程序:用于将大小为
n
的数组移位的程序code> 向右的d
元素:You can use the Below codes for shifting not rotating:
Programm for shifting array of size
n
byd
elements towards left:Programm for shifting array of size
n
byd
elements towards right:这样就可以了
This way it would work