I need to rearrange a list of items so that the selected item goes to the end of the list and the last item replaces the previous one and the previous item replaces the one before it and so on.
For example, if I have a list of ten items and the selected item is in position 5, this item goes to position 9 and 9 will replace 8 then 8 replaces 7 and 7 replaces 6 and six takes position 5. I managed to get the desired result using this code:
List<int> numList = new List<int>();
int selectedNum = 5;//Selected at runtime
for (int i = 0; i < 10; i++) numList.Add(i);
int numListCount = numList.Count-1;
int tempNum = numList[numListCount];
List<int> tempList = numList.GetRange(selectedNum + 1,(numList.Count-selectedNu开发者_Go百科m) - 2);
numList[numListCount] = selectedNum;
numList.RemoveRange(selectedNum, (numList.Count-selectedNum)-1);
numList.InsertRange(selectedNum, tempList);
numList.Insert(numListCount - 1, tempNum);
The result is:
0,1,2,3,4,6,7,8,9,5
I'm sure my code is ugly and inefficient: I have two questions:
- Is it possible to get the same result using Lambda?If not, then
- How can I refine my code. Thanks.
You can use the Remove Method to remove the selected item and the Add Method to append it at the end:
List<int> numList = new List<int>();
for (int i = 0; i < 10; i++) numList.Add(2 * i);
int selectedNum = 6; // selected at runtime
numList.Remove(selectedNum);
numList.Add(selectedNum);
Before:
0 2 4 6 8 10 12 14 16 18
After Remove(6):
0 2 4 8 10 12 14 16 18
After Add(6):
0 2 4 8 10 12 14 16 18 6
If you want to move the item at a selected index, you can use the RemoveAt Method instead of the Remove Method:
List<int> numList = new List<int>();
for (int i = 0; i < 10; i++) numList.Add(2 * i);
int selectedIndex = 5; // selected at runtime
int selectedNum = numList[selectedIndex];
numList.RemoveAt(selectedIndex);
numList.Add(selectedNum);
Before:
0 2 4 6 8 10 12 14 16 18
After RemoveAt(5):
0 2 4 6 8 12 14 16 18
After Add(10):
0 2 4 6 8 12 14 16 18 10
Using LINQ, you would create a new list that has the selected item removed and appended. An in-place update as shown above is much more efficient though.
List<int> numList = new List<int>();
for (int i = 0; i < 10; i++) numList.Add(2 * i);
int selectedIndex = 5; // selected at runtime
List<int> newNumList = numList.Take(selectedIndex)
.Concat(numList.Skip(selectedIndex + 1))
.Concat(numList.Skip(selectedIndex).Take(1))
.ToList();
numList:
0 2 4 6 8 10 12 14 16 18
newNumList:
0 2 4 6 8 12 14 16 18 10
From what I understand you just want to move a given item to the bottom of a list right?
List<int> list = new List<int>();
for (int i = 0; i < 10; i++)
numList.Add(i);
int temp = list[5];
list.RemoveAt(5);
list.Add(temp);
EDIT: My understanding was that you knew the position of the item you wanted to move (5). If you know the value then the other answer posted is the right one for that
You don't need all that extra stuff including a temp list. You can just do
numList.Remove(selectedNum);
numList.Add(selectedNum);
Simple as that.
Cannot check it now, but this should do the trick:
var temp = list.Take(index-1).Concat(list.Skip(index)).Concat(list[index]);
list = temp;
精彩评论