How do I swap 2 elements in a list [duplicate]

2019-05-27 11:17发布

Possible Duplicate:
Swap two items in List<T>

Edit: Maybe this will work for getting the 'b' value?

for (int i = 0; i < inventory.Count; i++)
{
    if (inventory[a].ItemRectangle.Intersects(inventory[i].ItemRectangle))
    {
        itemB = inventory[i];
    }
}

Edit: Here's my progress.

Item itemA;
Item itemB;

int a = -1;
int b = -1;

if (a != -1 && b != -1)
{
    itemA = inventory[a];
    itemB = inventory[b];

    Swap(ref itemA, ref itemB);

    inventory[a] = itemA;
    inventory[b] = itemB;
}

And here's is where I'm getting the 'a' value.

if (item.ItemSelected == true)
{
    a = item.ItemIndex;
}
else
    a = -1;

I haven't figured out how to get the 'b' value because I would have to check for an item colliding with another item that are both in the same list. If anybody know how I can do this, please tell me. It would look something like this I guess:

if (item.ItemRectangle.Intersects(//the other item.ItemRectangle)
{
    b = item.ItemIndex;
}
else
    b = -1;

I've made a List < Item > called inventory. So now I want to implement a swap function, like this:

foreach (Item item in inventory)
{
    if (mouseRectangle.Intersects(item.ItemRectangle))
    {
        if (Input.EdgeDetectLeftMouseDown())
        {
            switch (item.ItemSelected)
            {
                case false:
                    item.ItemSelected = true;
                    break;
                case true:
                    item.ItemSelected = false;
                    break;
            }
        }  
    }
    else if (Input.EdgeDetectLeftMouseDown())
    {
        switch (item.ItemSelected)
        {
            case true:
                item.ItemSelected = false;
                break;
        }
    }
    else if (item.ItemSelected == true)
    {
        item.ItemPosition = new Vector2(mouseRectangle.X, mouseRectangle.Y);
        item.ItemRectangle = new Rectangle(mouseRectangle.X, mouseRectangle.Y, 32, 32);
    }
    else if (item.ItemSelected == false && //a lot of checks to determine it is not intersecting with an equip slot
    {
        item.ItemPosition = item.OriginItemPosition;
        item.ItemRectangle = item.OriginItemRectangle;
    }
    else if (item.ItemRectangle.Intersects(item.ItemRectangle))
    {
        //SwapItem(inventory, item, item);
    }

So that's the part of the code I need help with. I want any item in the list to be able to swap with any other item in the list. My SwapItem method is just a placeholder, I dont actually have a SwapItem method yet.

I want the arguments that you pass in to the method to be related to the items I want to swap. So the first item would be the item that I have selected with my mouse, and the other item should be the item that the first item is intersecting with.

标签: c# list xna swap
2条回答
时光不老,我们不散
2楼-- · 2019-05-27 11:50

To swap an element of the list you can write an extension method as.

public static class ExtensionMethods
{
    public static void Swap<T>(this List<T> list, int index1, int index2)
    {
         T temp = list[index1];
         list[index1] = list[index2];
         list[index2] = temp;
    }
}

Remember to put the extension method inside a static class.

then you can do:

yourList.Swap(0,1); // swap element at index 0 with element at index 1
查看更多
何必那么认真
3楼-- · 2019-05-27 12:04

To swap the values of two variables, the easiest method is using references. This is a classic pointer exercise in c++, but it can apply to C# as well.

// Replace int with any data type / class you need
void Swap (ref int a, ref int b)
{
   int c = a; 
   a = b; 
   b = c;
}

The algorithm used is very simple, and the explanation is usually done like this: you have two glasses, one with water, and one with oil. To put the oil in the first glass, you will need to use a third glass, put the water inside, then put the oil in the first glass, and the water in the second one.


Here is what I had in mind. Look for the comments, so you can understand what's going on.:

// Unlike foreach, with for I can change the values in the list
for (int i = 0; i < inventory.Count; i++)
{
    if (mouseRectangle.Intersects(inventory[i].ItemRectangle))
    {
        if (Input.EdgeDetectLeftMouseDown())
        {
            // You can replace the switch with this shorter structure
            // if A is a bool value, !A will have the opposite value
            inventory[i].ItemSelected = !inventory[i].ItemSelected;
        }  
    }
    else if (Input.EdgeDetectLeftMouseDown())
    {
        // You don't need a case-switch for a single condition. An if should suffice
        if (inventory[i].ItemSelected) 
            inventory[i].ItemSelected = false;
    }
    else if (inventory[i].ItemSelected == true)
    {
        inventory[i].ItemPosition = new Vector2(mouseRectangle.X, mouseRectangle.Y);
        inventory[i].ItemRectangle = new Rectangle(mouseRectangle.X, mouseRectangle.Y, 32, 32);
    }
    else if (inventory[i].ItemSelected == false && //a lot of checks to determine it is not intersecting with an equip slot
    {
        inventory[i].ItemPosition = inventory[i].OriginItemPosition;
        inventory[i].ItemRectangle = inventory[i].OriginItemRectangle;
    }

    // Something definitely wrong with this line, a rectangle to instersect with itself??
    else if (inventory[i].ItemRectangle.Intersects(inventory[PROBABLY_SOMETHING_ELSE].ItemRectangle))
    {
        Swap (ref inventory[i], ref inventory[PROBABLY_SOMETHING_ELSE])
    }
}
查看更多
登录 后发表回答