移动元素的数组用C# [英] Moving element in array with C#
本文介绍了移动元素的数组用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么办法来移动一个阵列内的物品?例如:
Is there any way to move items inside an array? For example:
int[] myArray = {1,2,3,4};
第二个元素成为最后一个:
2nd element becomes the last:
int[] myArray = {1,3,4,2};
P.S:不,这不是一门功课。我能想到至少有一个解决方案,但它需要相当难实现:
P.S.: No that's not a homework. I can think of at least one solution but it requires rather difficult implementation:
- 首先,我们第二个元素保存到智力
- 那么,我们从数组 删除此元素
- 然后我们添加新的元素在我的数组的结尾
任何其他(读 - 容易)?的方式来做到这一点。
Any other (read - easier) way to do this?
推荐答案
有没有简单的方法使用数组来做到这一点。你会通过数组的每一个元素转移到了在动指数有环,然后重新插入尾部的元素。你总是可以使用列表与LT; INT方式>
来做到这一点。
There is no easy way to do it using an array. You'll have to loop through the array shifting every element up to the index that's moving, and then re-insert that element at the end. You could always use a List<int>
to do it.
List<int> list = myArray.ToList();
int value = list[1];
list.RemoveAt(1);
list.Add(value);
myArray = list.ToArray();
这篇关于移动元素的数组用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文