Подтвердить что ты не робот

Получить предыдущий/следующий элемент данного элемента в списке <>

Говорит, у меня есть этот список: 1, 3, 5, 7, 9, 13

Например, заданное значение: 9, предыдущий элемент - 7, а следующий - 13

Как я могу добиться этого с помощью С#?

4b9b3361

Ответ 1

Вы можете использовать индексатор для получения элемента по желаемому индексу. Добавление одного к индексу приведет вас к следующему, и вычитание из индекса даст вам предыдущий элемент.

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

Вам нужно будет проверить, существует ли следующий и предыдущий индекс, другие разумные, вы получите IndexOutOfRangeException исключение. Поскольку List является индексом на основе нуля, поэтому первый элемент будет иметь индекс 0, а второй - 1 и т.д.

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

Ответ 2

        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

Ответ 3

Я реализовал это, унаследовав .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]; }
        }
    }

Используя это становится довольно легко

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

Ответ 4

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;

Ответ 5

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

Ответ 6

Может быть полезно

 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;

Ответ 7

Чтобы сделать это своего рода Круговым списком, попробуйте следующее:

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

Ответ 8

Использование 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);

Ответ 9

Это можно сделать с помощью 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();

Ответ 10

Подход с 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);
}