Get the previous / next element of this element in the list <>

Says I have this list: 1, 3, 5, 7, 9, 13

For example, the set value is 9, the previous item is 7, and the next is 13

How can I achieve this with C #?

+18
source share
10 answers

You can use the indexer to get the item at the desired index. Adding one to the index will lead you to the next, and subtracting from the index will give you the previous item.

 int index = 4; int prev = list[index-1]; int next = list[index+1]; 

You will need to check if the next and previous index exists, others are reasonable, you will get an IndexOutOfRangeException exception. Since List is an index based on zero , therefore the first element will have index 0 , and the second 1 will have 1 , etc.

 if(index - 1 > -1) prev = list[index-1]; if(index + 1 < list.Length) next = list[index+1]; 
+26
source
  List<int> listInts = new List<int>(); listInts.AddRange(new int[] { 1, 3, 5, 7, 9, 13 }); int index = listInts.IndexOf(3); //The index here would be "1" index++; //Check first if the index is in the length int element = listInts[index]; //element = 5 
+6
source

I implemented this by inheriting the .Net List

 public class NavigationList<T> : List<T> { private int _currentIndex = 0; public int CurrentIndex { get { if (_currentIndex > Count - 1) { _currentIndex = Count - 1; } if (_currentIndex < 0) { _currentIndex = 0; } return _currentIndex; } set { _currentIndex = value; } } public T MoveNext { get { _currentIndex++; return this[CurrentIndex]; } } public T MovePrevious { get { _currentIndex--; return this[CurrentIndex]; } } public T Current { get { return this[CurrentIndex]; } } } 

Using it becomes pretty easy

  NavigationList<string> n = new NavigationList<string>(); n.Add("A"); n.Add("B"); n.Add("C"); n.Add("D"); Assert.AreEqual(n.Current, "A"); Assert.AreEqual(n.MoveNext, "B"); Assert.AreEqual(n.MovePrevious, "A"); 
+5
source
 int index = list.IndexOf(9); // find the index of the given number // find the index of next and the previous number // by taking into account that // the given number might be the first or the last number in the list int prev = index > 0 ? index - 1 : -1; int next = index < list.Count - 1 ? index + 1 : -1; int nextItem, prevItem; // if indexes are valid then get the items using indexer // otherwise set them to a temporary value, // you can also use Nullable<int> instead nextItem = prev != -1 ? list[prev] : 0; prevItem = next != -1 ? list[next] : 0; 
+3
source
 var index = list.IndexOf(9); if (index == -1) { return; // or exception - whater, no element found. } int? nextItem = null; //null means that there is no next element. if (index < list.Count - 1) { nextItem = list[index + 1]; } int? prevItem = null; if (index > 0) { prevItem = list[index - 1]; } 
+3
source

May be useful

  int NextValue = 0; int PreviousValue =0; int index = lstOfNo.FindIndex(nd =>nd.Id == 9); var Next = lstOfNo.ElementAtOrDefault(index + 1); var Previous = lstOfNo.ElementAtOrDefault(index - 1); if (Next != null) NextValue = Next; if (Previous != null) PreviousValue = Previous; 
+2
source

To make this a kind of Circular List, try the following:

 public class NavigationList<T> : List<T> { private int _currentIndex = -1; public int CurrentIndex { get { if (_currentIndex == Count) _currentIndex = 0; else if (_currentIndex > Count - 1) _currentIndex = Count - 1; else if (_currentIndex < 0) _currentIndex = 0; return _currentIndex; } set { _currentIndex = value; } } public T MoveNext { get { _currentIndex++; return this[CurrentIndex]; } } public T Current { get { return this[CurrentIndex]; } } } 
+1
source

Using LINQ on a single line and circular search:

Next of

 YourList.SkipWhile(x => x != NextOfThisValue).Skip(1).DefaultIfEmpty( YourList[0] ).FirstOrDefault(); 

Previous of

 YourList.TakeWhile(x => x != PrevOfThisValue).DefaultIfEmpty( YourList[YourList.Count-1]).LastOrDefault(); 

This is a working example (link to violin)

  List<string> fruits = new List<string> {"apple", "banana", "orange", "raspberry", "kiwi"}; string NextOf = "orange"; string NextOfIs; NextOfIs = fruits.SkipWhile(x => x!=NextOf).Skip(1).DefaultIfEmpty(fruits[0]).FirstOrDefault(); Console.WriteLine("The next of " + NextOf + " is " + NextOfIs); NextOf = "kiwi"; NextOfIs = fruits.SkipWhile(x => x!=NextOf).Skip(1).DefaultIfEmpty(fruits[0]).FirstOrDefault(); Console.WriteLine("The next of " + NextOf + " is " + NextOfIs); string PrevOf = "orange"; string PrevOfIs; PrevOfIs = fruits.TakeWhile(x => x!=PrevOf).DefaultIfEmpty(fruits[fruits.Count-1]).LastOrDefault(); Console.WriteLine("The prev of " + PrevOf + " is " + PrevOfIs); PrevOf = "apple"; PrevOfIs = fruits.TakeWhile(x => x!=PrevOf).DefaultIfEmpty(fruits[fruits.Count-1]).LastOrDefault(); Console.WriteLine("The prev of " + PrevOf + " is " + PrevOfIs); 
+1
source

This can be done using LinkedList<T>

 List<int> intList = new List<int> { 1, 3, 5, 7, 9, 13 }; LinkedList<int> intLinkedList = new LinkedList<int>(intList); Console.WriteLine("Next Value to 9 "+intLinkedList.Find(9).Next.Value); Console.WriteLine("Next Value to 9 " +intLinkedList.Find(9).Previous.Value); //Consider using dictionary for frequent use var intDictionary = intLinkedList.ToDictionary(i => i, i => intLinkedList.Find(i)); Console.WriteLine("Next Value to 9 " + intDictionary[9].Next.Value); Console.WriteLine("Next Value to 9 " + intDictionary[9].Previous.Value); Console.Read(); 
0
source

Approach with ElementOrDefault()

https://dotnetfiddle.net/fxVo6T

 int?[] items = { 1, 3, 5, 7, 9, 13 }; for (int i = 0; i < items.Length; i++) { int? previous = items.ElementAtOrDefault(i - 1); int? current = items.ElementAtOrDefault(i); int? next = items.ElementAtOrDefault(i + 1); } 
0
source

All Articles