Get values ​​from next and previous items in a C # sorted list

I have a sorted list that will go through two elements and compare them. Is there a function in the SortedList class in C # that will do the next and previous? I got some help with .Skip, but since the keys would be variable, how does it work? All I have to do is take the first element and the second element, and then go to the third, fourth, fifth and sixth, etc. I would like it to be as simple as LinkedList ".next.next."

  double velocity = positionList.Values.Skip(1);

Edit: PositionList is a type

   <double, HandCoordinate>
   HandCoordinate = {double, double, double}

Does it help?

Thank!

+5
source share
3 answers
  List<int> ints = new List<int>();
  ints.Add(1);
  ints.Add(2);
  ints.Add(3);
  ints.Add(4);
  for (int i = 0; i < ints.Count; i += 2)
  {
    var pair = ints.Skip(i).Take(2);
    var first = pair.First();
    var last = pair.Last();
  }

. , . .

Skip().

var pair = new { First = ints[i], Second = ints[i += 1] };
0

. , ?

, IEnumerable:

using System;
using System.Collections.Generic;

namespace Demo
{
    internal static class Program
    {
        public static void Main()
        {
            double[] test = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

            foreach (var pair in test.AsPairs()) // This is how you use it.
            {
                Console.WriteLine("({0}, {1})", pair.Item1, pair.Item2);
                // Or simply: Console.WriteLine(pair);
            }
        }
    }

    public static class EnumerableExt
    {
        public static IEnumerable<Tuple<T, T>> AsPairs<T>(this IEnumerable<T> sequence)
        {
            bool isFirst = true;
            T first = default(T);

            foreach (var item in sequence)
            {
                if (isFirst)
                {
                    first = item;
                    isFirst = false;
                }
                else
                {
                    isFirst = true;
                    yield return new Tuple<T, T>(first, item);
                }
            }
        }
    }
}
0

The SortedList class inherits from IEnumerator , so you can use it:

SortedList list = ...
var listEnumerator = ((IEnumerable)list).GetEnumerator();
Pair<MyType> pair = null
do
{
    pair = Pair.Next<MyType>(listEnumerator);
    ...
}
while(pair != null)

...

class Pair<T>
{
    public T First {get; set;}
    public T Second {get; set;}

    public static Pair<T> Next<T>(IEnumerator enumerator)
    {
        var first = enumerator.Current;
        if(enumerator.MoveNext())
        {
           return new Pair<T>
               {
                   First = (T)first,
                   Second = (T)enumerator.Current,
               }
        }
        return null;
    }
}
0
source

All Articles