C# quickest way to shift array

前端 未结 20 1316
礼貌的吻别
礼貌的吻别 2020-12-01 01:35

How can I quickly shift all the items in an array one to the left, padding the end with null?

For example, [0,1,2,3,4,5,6] would become [1,2,3,4,5,6,null]

Ed

20条回答
  •  旧时难觅i
    2020-12-01 02:14

    Can't you

    • allocate the array with an extra 1000 elements

    • have an integer variable int base = 0

    • instead of accessing a[i] access a[base+i]

    • to do your shift, just say base++

    Then after you've done this 1000 times, copy it down and start over.
    That way, you only do the copy once per 1000 shifts.


    Old joke:
    Q: How many IBM 360s does it take to shift a register by 1 bit?
    A: 33. 32 to hold the bits in place, and 1 to move the register. (or some such...)

提交回复
热议问题