【问题标题】:Getting an array's index from custom valued indexed array从自定义值索引数组中获取数组的索引
【发布时间】:2011-08-24 13:26:41
【问题描述】:
SortedList<string, int> list = new SortedList<string, int>;
list.Add("abc", 2);
list.Add("def", 3);

string index = list.GetIndex(2) ???????
Console.WriteLine(index);

我真的很困惑。如何使用索引的值获取数组的索引??

输出应该是

abc

解决方案:

使用SortedList公开的IndexOfValue方法,结合Linq扩展方法ElementAt(int index),所以:

string index = list.ElementAt(list.IndexOfValue(value)).Key;//output will be abc

---o---

这也有效

string index = list.Keys.ElementAt<string>(list.IndexOfValue(2));

【问题讨论】:

  • 我和你一样困惑。什么阵法?我没有看到任何数组。
  • 如果 2 和 3 只是索引引用,为什么不直接使用字符串列表,或者 int, string 的字典?
  • 实际上我有一个以 ToolStripMenuItem 为索引和 Form 作为值的 SortedList。

标签: c#


【解决方案1】:

使用SortedList暴露的IndexOfValue方法,结合Linq扩展方法ElementAt(int index),所以:

string index = list.ElementAt(list.IndexOfValue(value)).Key;//output will be abc

【讨论】:

  • 谢谢!这也有效(我刚刚找到) string index = list.Keys.ElementAt(list.IndexOfValue(2));
【解决方案2】:

关于 SortedList.GetByIndex 方法的 MSDN 文档:

using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( 1.3, "fox" );
      mySL.Add( 1.4, "jumped" );
      mySL.Add( 1.5, "over" );
      mySL.Add( 1.2, "brown" );
      mySL.Add( 1.1, "quick" );
      mySL.Add( 1.0, "The" );
      mySL.Add( 1.6, "the" );
      mySL.Add( 1.8, "dog" );
      mySL.Add( 1.7, "lazy" );

      // Gets the key and the value based on the index.
      int myIndex=3;
      Console.WriteLine( "The key   at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) );
      Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) );

      // Gets the list of keys and the list of values.
      IList myKeyList = mySL.GetKeyList();
      IList myValueList = mySL.GetValueList();

      // Prints the keys in the first column and the values in the second column.
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < mySL.Count; i++ )
         Console.WriteLine( "\t{0}\t{1}", myKeyList[i], myValueList[i] );
   }
}
/* 
This code produces the following output.

The key   at index 3 is 1.3.
The value at index 3 is fox.
    -KEY-    -VALUE-
    1    The
    1.1    quick
    1.2    brown
    1.3    fox
    1.4    jumped
    1.5    over
    1.6    the
    1.7    lazy
    1.8    dog
*/ 

【讨论】:

【解决方案3】:

根据您的输出应该是abc 的说法,我认为您真的想要text,而不是index。要根据 value 获取项目的文本,只需执行以下操作:

SortedList<string, int> list = new SortedList<string, int>();
list.Add("abc", 2);
list.Add("def", 3);
string text = list.ElementAt(listz.IndexOfValue(2)).Key
Console.WriteLine(text); 

【讨论】:

  • SortedList 中没有类似 Item 的属性 -_-
  • 参数无效。列表基于字符串而不是整数。
  • 对不起,你说得对。现在是正确的!我对其进行了测试,它返回abc
【解决方案4】:

使用 SortedList.Values 属性获取包含 SortedList 对象中的值的 ICollection 对象。

【讨论】:

    【解决方案5】:

    试一试:

    SortedList<string, int> list = new SortedList<string, object>();
    list.Add("One", 1);
    list.Add("Two", 2);                               
    
    int value = (int)list.ElementAt(1).Value;
    

    或者……

    int value = list.IndexOfKey("One");
    

    【讨论】:

      猜你喜欢
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2020-06-19
      相关资源
      最近更新 更多