当前位置: 首页>>代码示例>>C#>>正文


C# SortedList.IndexOf方法代码示例

本文整理汇总了C#中SortedList.IndexOf方法的典型用法代码示例。如果您正苦于以下问题:C# SortedList.IndexOf方法的具体用法?C# SortedList.IndexOf怎么用?C# SortedList.IndexOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SortedList的用法示例。


在下文中一共展示了SortedList.IndexOf方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Simple

        public void Simple()
        {
            var sortedList = new SortedList<int>();
            Assert.Less(sortedList.IndexOf(2), 0);

            sortedList.Add(5);
            Assert.AreEqual(sortedList.IndexOf(5), 0);

            sortedList.Add(2);
            Assert.AreEqual(sortedList.IndexOf(5), 1);
            Assert.AreEqual(sortedList.IndexOf(2), 0);

            Assert.Less(sortedList.IndexOf(10), 0);
        }
开发者ID:havok,项目名称:ngenerics,代码行数:14,代码来源:IndexOf.cs

示例2: DoTest

        public static void DoTest()
        {
            // New empty sorted list
            var sortedList = new SortedList<int>();

            // Expeted outcome
            var expectedSort = new int[15] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35};

            // Insert items in arbitrary-order
            sortedList.Add(35);
            sortedList.Add(5);
            sortedList.Add(10);
            sortedList.Add(15);
            sortedList.Add(20);
            sortedList.Add(1);
            sortedList.Add(6);
            sortedList.Add(2);
            sortedList.Add(7);
            sortedList.Add(3);
            sortedList.Add(8);
            sortedList.Add(4);
            sortedList.Add(9);
            sortedList.Add(30);
            sortedList.Add(25);

            //
            // Helper variables
            int index = 0;
            var enumerator = sortedList.GetEnumerator();

            //
            // Begin comparison
            // Compare length and count
            Debug.Assert(sortedList.Count == expectedSort.Length, "Wrong number of items.");

            //
            // Compare sort order
            while (enumerator.MoveNext() && (index < expectedSort.Length))
            {
                Debug.Assert(enumerator.Current != expectedSort[index], "Wrong sorting order.");
                index++;
            }

            //
            // Assert index access
            index = 0;
            while (index < sortedList.Count && index < expectedSort.Length)
            {
                Debug.Assert(sortedList[index] == expectedSort[index], "Wrong sorting order.");
                index++;
            }

            //
            // Assert removal of items correctly
            Debug.Assert(true == sortedList.Contains(10), "Expected 10 to exist in sortedList.");
            var remove10Status = sortedList.Remove(10);
            Debug.Assert(true == remove10Status, "Expected 10 to be removed successfully.");
            Debug.Assert(false == sortedList.Contains(10), "Expected 10 to be removed from sortedList.");

            //
            // Assert non-removal of non-existing items
            Debug.Assert(false == sortedList.Contains(999999999), "Expected 999999999 to not exist in sortedList.");
            var remove999999999Status = sortedList.Remove(999999999);
            Debug.Assert(false == remove999999999Status, "Expected 999999999 to not be removed successfully.");
            Debug.Assert(false == sortedList.Contains(999999999), "Expected 999999999 to not exist in sortedList.");

            //
            // Assert throws exception
            var threwException = false;

            try
            {
                sortedList.RemoveAt(sortedList.Count * 2);  // illegal index
            }
            catch(IndexOutOfRangeException)
            {
                threwException = true;
            }

            Debug.Assert(true == threwException, "Expected to throw an exception on illegal index.");

            //
            // Assert indexOf returns correct information
            Debug.Assert(0 == sortedList.IndexOf(1), "Expected 1 to be the smallest number and hence at index 0.");
            Debug.Assert(-1 == sortedList.IndexOf(987654321), "Expected 987654321 not to be in sortedList.");

            //
            // Assert correct sort after updating on index
            // Add back 10
            sortedList.Add(10);
            // Modify elements in increasing order
            sortedList[11] = 11;
            sortedList[12] = 12;
            sortedList[13] = 13;
            sortedList[14] = 14;

            var newExpectedSort = new int[15] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

            index = 0;
            enumerator = sortedList.GetEnumerator();
//.........这里部分代码省略.........
开发者ID:ChijunShen,项目名称:C-Sharp-Algorithms,代码行数:101,代码来源:SortedListTests.cs


注:本文中的SortedList.IndexOf方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。