簡體   English   中英

獲取 List<> 中給定項目的上一個/下一個項目

[英]Get previous/next item of a given item in a List<>

說我有這個列表:1、3、5、7、9、13

例如給定值為:9,上一項為7,下一項為13

如何使用 C# 實現此目的?

您可以使用索引器獲取所需索引處的元素。 向索引添加一個將獲得一個,從索引中減去一個將為您提供上一個元素。

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

您必須檢查下一個和上一個索引是否存在,否則您將收到IndexOutOfRangeException異常。 由於 List 是基於零的索引,因此第一個元素的索引為0 ,第二個元素的索引0 1 ,依此類推。

if(index - 1 > -1)
   prev = list[index-1];
if(index + 1 < list.Length)
   next = list[index+1];

在一行中使用 LINQ 並進行循環搜索:

下一個

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

上一個

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

這是一個工作示例(鏈接到小提琴)

    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);

我已經通過繼承 .Net 列表實現了這一點

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]; }
        }
    }

使用這個變得很容易

 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");
        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
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;
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];
}

此外,如果您想要具有循環邏輯的緊湊解決方案而不創建新列表,您可以使用以下代碼:

int nextNumber = list[(list.IndexOf(currentNumber) + 1) % list.Count];
int previousNumber = list[(list.IndexOf(currentNumber) - 1 + list.Count) % list.Count];

https://dotnetfiddle.net/PkP2Jy

要使其成為一種循環列表,請嘗試以下操作:

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]; }
    }
}

以下可能會有所幫助

 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;

這可以使用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();

這是結合@Thunder@Melad答案獲得的完整圓圈列表:

private class CircularList<T> : List<T>
    {
        private int _currentIndex = 0;
        public int CurrentIndex
        {
            get
            {
                if (_currentIndex > Count - 1) { _currentIndex = 0; }
                if (_currentIndex < 0) { _currentIndex = Count - 1; }
                return _currentIndex;
            }
            set => _currentIndex = value;
        }
        
        public int NextIndex
        {
            get
            {
                if (_currentIndex == Count - 1) return 0;
                return _currentIndex + 1;
            }
        }
        
        public int PreviousIndex
        {
            get
            {
                if (_currentIndex == 0) return Count - 1;
                return _currentIndex - 1;
            }
        }
        
        public T Next => this[NextIndex];

        public T Previous => this[PreviousIndex];

        public T MoveNext
        {
            get { _currentIndex++; return this[CurrentIndex]; }
        }

        public T MovePrevious
        {
            get { _currentIndex--; return this[CurrentIndex]; }
        }

        public T Current => this[CurrentIndex];
        
        
    }
}

在此處使用示例檢查 git-repo

使用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);
}

如果找不到該項目或者它是列表中的最后一個元素,則返回 null

 List<string> fruits = new List<string> { "apple", "banana",  "kiwi" };
    var index = fruits.IndexOf("banana");
    var nextFruit = index==-1 ?null : fruits.Skip(index+1).FirstOrDefault();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM