C#向右移动数组 [英] C# shifting array to right

查看:497
本文介绍了C#向右移动数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个问题,我确实成功地向左移动,但是我向右移动时遇到了问题
这就是我向左移动的方式:

so I have a problem I did shifting to left successfully but I have problems with shifting to right This is how I shift to the left:

示例我的意思是左右移动:

Example what I mean shift to left and right:


  • 您有一个数组:1,2,3,4,5,

  • 向左移动一,2、3、4、5、1

  • 向右移动一:5、1、2、3、4,

推荐答案

向右移动非常相似,因此向左移动,您只需要保存 last 元素而不是 first 元素,然后从末尾开始依次迭代:

Shifting to the right is quite similar so shifting to the left, you just need to save the last element instead of the first one, and iterate from the end backwards:

int t = tab[tab.Length - 1];
for (int i = tab.Length - 1; i > 0; i--)
    tab[i] = tab[i - 1];
tab[0] = t;

这篇关于C#向右移动数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆