</pre><pre name="code" class="csharp">最近在写棋牌房间座位号 处理, 服务器发送来座位号跟 玩家信息
</pre><pre name="code" class="csharp">但是要把当前用户显示在正中央,所以必须把服务器发来的座位号根据当前用户的座位号排列。
</pre><pre name="code" class="csharp">如果当前用户座位号位2 那么,要把服务器发来的座位号根据 2 来重新排列 ,
</pre><pre name="code" class="csharp">座位号数组向左平移俩个位置,下面就是平移数组的方法。
</pre><pre name="code" class="csharp">其实挺简单, 就是把向左平移的2个单位 先保存在临时数组中,
</pre><pre name="code" class="csharp">把剩下的座位 添加在 结果数组中 ,然后把临时数组中的数据跟在结果数组中。
</pre><pre name="code" class="csharp">将数组平移若干个位置
</pre><pre name="code" class="csharp">
class Program {
static void Main(string[] args) {
int nr = 99;<span style="white-space:pre"> </span>//向左平移单位
int[] orign = new int[] { 1, 2, 3, 4, 5 };<span style="white-space:pre"> </span>//原始数组
int[] temp = new int[nr];<span style="white-space:pre"> </span>//临时数组
int[] result = new int[orign.Length];<span style="white-space:pre"> </span>//结果数组
nr = nr % orign.Length;<span style="white-space:pre"> </span>//偏移单位根据原始数组取余
for (int i = 0; i < nr; i++) {
temp[i] = orign[i];
}
for (int i = 0; i < orign.Length - nr; i++) {
result[i] = orign[i + nr];
}
for (int i = 0; i < nr; i++) {
result[orign.Length - nr + i] = temp[i];
}
for (int i = 0; i < result.Length; i++) {
Console.WriteLine(result[i]);
}
Console.ReadKey();
}
}
输出结果 向左平移了 99 个单位