2009-04-22 130 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];