2009-04-22 134 views
13

我想获取列表中的下一个元素,并且如果列表处于它的末尾,我需要第一个元素。 所以我只是想让它换成其他的字。List <>获取下一个元素或获取第一个

List<int> agents = taskdal.GetOfficeAgents(Branches.aarhusBranch); 
    if (lastAgentIDAarhus != -1) 
    { 
     int index = agents.IndexOf(lastAgentIDAarhus); 
     if (agents.Count > index + 1) 
     { 
      lastAgentIDAarhus = agents[index + 1]; 
     } 
     else 
     { 
      lastAgentIDAarhus = agents[0]; 
     } 
    } 
    else 
    { 
     lastAgentIDAarhus = agents[0]; 
    } 

我与上面显示自己的解决方案很不高兴,让我知道,如果你有一个更好的:)

回答

20
lastAgentIDAarhus = agents[index == -1 ? 0 : index % agents.Count]; 

使用MOD运算符的atuomatically砍下索引到可能的索引的范围内。

模运算符是对DIV(/)运算符的补充,并返回两个整数除法的其余部分。例如,如果将9乘以6,则结果为1,余数为3.MOD操作员要求3.

+0

哇,这是一个非常整洁的解决方案,如果这样的作品:) 小心解释一下? – 2009-04-22 11:41:29

+0

模运营商做模,这正是你想要的。如果你可以初始化索引为0而不是-1,你可以使用:lastAgentIDAarhus = agents [index%(agents.Count - 1)] – configurator 2009-04-22 15:02:14

+3

我认为它应该是'index%(agents.Count)' – 2012-08-11 22:37:42

2

不是一个很大的区别,但至少有一些较少的代码(至少在编辑器); O)

List<int> agents = taskdal.GetOfficeAgents(Branches.aarhusBranch); 
if (lastAgentIDAarhus != -1) 
{ 
    int index = agents.IndexOf(lastAgentIDAarhus); 
    lastAgentIDAarhus = (agents.Count > index + 1 ? agents[index + 1] : agents[0]); 
} 
else 
{ 
    lastAgentIDAarhus = agents[0]; 
} 
4

作为一个略有不同的看法,下面是一个扩展方法,您可以使用它来创建任何IEnumerable'圆形”

public static IEnumerable<T> AsCircularEnumerable<T>(this IEnumerable<T> enumerable) 
    { 
    var enumerator = enumerable.GetEnumerator(); 
    if(!enumerator.MoveNext()) 
     yield break; 

    while (true) 
    { 
     yield return enumerator.Current; 
     if(!enumerator.MoveNext()) 
     enumerator = enumerable.GetEnumerator(); 
    } 
    } 

,所以你可以使用这样的

var agents = new List<int> {1, 2, 3, 4, 123, 234, 345, 546}; 

    foreach(var i in agents.AsCircularEnumerable()) 
    { 
    Console.WriteLine(i); 
    } 

这只会继续下去... :)

11

或者干脆:

public static T NextOf<T>(this IList<T> list, T item) 
{ 
    return list[(list.IndexOf(item) + 1) == list.Count ? 0 : (list.IndexOf(item) + 1)]; 
} 

实施例:

List<string> names = new List<string>(); 

names.Add("jonh"); 
names.Add("mary"); 

string name = String.Empty;  

name = names.NextOf(null); //name == jonh 

name = names.NextOf("jonh"); //name == mary 

name = names.NextOf("mary"); //name == jonh 
3

我喜欢使用一个扩展方法的Vinicius's idea。不幸的是,他发布的代码会抛出异常。这是基于他的,但它不会抛出一个异常,这是非常简单,易于阅读(恕我直言):

public static T Next<T>(this IList<T> list, T item) 
{ 
    var nextIndex = list.IndexOf(item) + 1; 

    if (nextIndex == list.Count) 
    { 
     return list[0]; 
    } 

    return list[nextIndex]; 
} 

它会返回列表中的第一个项目如果传入的项目不在该名单,因为list.IndexOf(item)将在这种情况下返回-1;

0

如果我们添加一些检查以避免常见错误,您认为如何?

public static class ListExtensions 
{ 
    public static TType Next<TType>(this IList<TType> list, TType item) 
    { 
     if (list == null) return default(TType); 

     var itemIndex = list.IndexOf(item); 
     if (itemIndex < 0) return list.FirstOrDefault(); 

     var nextIndex = itemIndex + 1; 

     return nextIndex >= list.Count 
      ? list.FirstOrDefault() 
      : list[nextIndex]; 
    } 
} 
-4

string line = lines [lines.FindIndex(x => x.Contains(item))+ 1];