Moving elements in array c#

后端 未结 4 1583
感情败类
感情败类 2021-01-12 07:59

I have this very simple array which I want to be able to move around some items in. Are there any built in tools in c# to do this? If not, du you have any suggestion in how

4条回答
  •  没有蜡笔的小新
    2021-01-12 08:27

    This solved my problem

    var arr = new ArrayList 
        {
            "a", "b", "c", "d", "e", "f"
        };
    var first = arr[2];  //pass  the value which you want move from 
    var next = arr[5]; //pass the location where you want to place
    var m = 0;
    var k = arr.IndexOf(first, 0, arr.Count);
    m = arr.IndexOf(next, 0, arr.Count);
    if (k > m)
    {
        arr.Insert(m, first);
        arr.RemoveAt(k + 1);
    }
    else
    {
        arr.Insert(k, next);
        arr.RemoveAt(m + 1);
    }
    

    returns a, b, f, c, d, e

提交回复
热议问题