Easiest way to Rotate a List in c#

后端 未结 16 3139
鱼传尺愫
鱼传尺愫 2020-12-01 07:06

Lists say I have a list List {1,2,3,4,5}

Rotate means:

=> {2,3,4,5,1} => {3,4,5,1,2} => {4,5,1,2,3}
16条回答
  •  失恋的感觉
    2020-12-01 07:46

    My solution maybe too basic (I wouldn't like to say it's lame...) and not LINQ'ish.
    However, it has a pretty good performance.

    int max = 5; //the fixed size of your array.
    int[] inArray = new int[5] {0,0,0,0,0}; //initial values only.
    
    void putValueToArray(int thisData)
    {
      //let's do the magic here...
      Array.Copy(inArray, 1, inArray, 0, max-1);
      inArray[max-1] = thisData;
    }
    

提交回复
热议问题