Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的循环列表#_C# - Fatal编程技术网

C# C语言中的循环列表#

C# C语言中的循环列表#,c#,C#,我对C#不是很有经验。我正在尝试建立循环列表,我是这样做的: public List<string> items = new List<string> {"one", "two", "three"}; private int index = 0; void nextItem() { if (index < items.Count - 1) index += 1; else index = 0; setIte

我对C#不是很有经验。我正在尝试建立循环列表,我是这样做的:

public List<string> items = new List<string> {"one", "two", "three"};
private int index = 0;

void nextItem() {
    if (index < items.Count - 1)
        index += 1;
    else
        index = 0;

    setItem();
}

void previousItem() {
    if (index > 0)
        index -= 1;
    else
        index = items.Count - 1;

    setItem();
}

void Update() {
         if (Input.GetKeyDown(KeyCode.RightArrow)) nextItem();
    else if (Input.GetKeyDown(KeyCode.LeftArrow))  previousItem();
}
公共列表项=新列表{“一”、“二”、“三”};
私有整数指数=0;
void nextItem(){
如果(索引0)
指数-=1;
其他的
索引=项目。计数-1;
setItem();
}
无效更新(){
if(Input.GetKeyDown(KeyCode.RightArrow))nextItem();
else if(Input.GetKeyDown(KeyCode.LeftArrow))previousItem();
}
但现在我想知道:我是在重新发明轮子吗?C#是否已经为此提供了适当的数据结构

编辑:以防需要某些上下文。我有一个游戏菜单,我在其中显示一组项目,我希望当我按下“下一步”并在最后一个项目上输入时,再次显示第一个项目。

使用您的代码变得非常简单:

void nextItem() {
    index++; // increment index
    index %= items.Count; // clip index (turns to 0 if index == items.Count)
    // as a one-liner:
    /* index = (index + 1) % items.Count; */

    setItem();
}

void previousItem() {
    index--; // decrement index
    if(index < 0) {
        index = items.Count - 1; // clip index (sadly, % cannot be used here, because it is NOT a modulus operator)
    }
    // or above code as a one-liner:
    /* index = (items.Count+index-1)%items.Count; */ // (credits to Matthew Watson)

    setItem();
}
void nextItem(){
index++;//增量索引
index%=items.Count;//剪辑索引(如果index==items.Count,则变为0)
//作为一个班轮:
/*索引=(索引+1)%items.Count*/
setItem();
}
作废以前的项目(){
索引--;//减量索引
如果(指数<0){
index=items.Count-1;//剪辑索引(遗憾的是,这里不能使用%s,因为它不是模数运算符)
}
//或以上代码作为一行代码:
/*索引=(items.Count+index-1)%items.Count;*//(归功于Matthew Watson)
setItem();
}

您也可以编写自己的循环列表

public class CircularList<T> : List<T>, IEnumerable<T>
{
    public new IEnumerator<T> GetEnumerator()
    {
        return new CircularEnumerator<T>(this);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return new CircularEnumerator<T>(this);
    }
}

class CircularEnumerator<T> : IEnumerator<T>
{
    private readonly List<T> list;
    int i = 0;

    public CircularEnumerator(List<T> list){
        this.list = list;
    }

    public T Current => list[i];

    object IEnumerator.Current => this;

    public void Dispose()
    {
        
    }

    public bool MoveNext()
    {
        i = (i + 1) % list.Count;
        return true;
    }

    public void Reset()
    {
        i = 0;
    }
}
公共类循环列表:列表,IEnumerable
{
公共新IEnumerator GetEnumerator()
{
返回新的循环消耗器(本);
}
IEnumerator IEnumerable.GetEnumerator()
{
返回新的循环消耗器(本);
}
}
类循环计数器:IEnumerator
{
私有只读列表;
int i=0;
公众传阅人(名单){
this.list=列表;
}
公共T电流=>列表[i];
对象IEnumerator.Current=>this;
公共空间处置()
{
}
公共图书馆
{
i=(i+1)%list.Count;
返回true;
}
公共无效重置()
{
i=0;
}
}

您的列表不是循环的,只是索引它的方法。你只是想通过一个环绕的列表建立索引,还是真的想要一个循环缓冲区?在这里使用队列更好。我有一个游戏,我在菜单中显示一个项目列表。当我到达最后一项时,我想再次显示第一项。@M.kazemAkhgary真的吗?对于
previousItem
,只支持在一端添加而在另一端删除的数据结构要好得多。您可以将下一个索引的计算简化为
index=(index+1)%items.Count。同样,前面索引的计算可以简化为
index=(items.Count+index-1)%items.Count(尽管在本文中,“简化”可能是一个相对的术语…)