首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套的SortedList

嵌套的SortedList
EN

Stack Overflow用户
提问于 2017-06-01 03:05:34
回答 2查看 226关注 0票数 0

我是C#的新手,来自PHP语言背景,在那里多维数组就像公园里的散步一样。

如何在C#中创建带有SortedList的嵌套/多维数组?我相信通过阅读,SortedList在功能方面可能是最具可比性的。我尝试了以下方法,但它抛出了错误:

代码语言:javascript
复制
SortedList arr = new SortedList();
arr.Add("hello",new SortedList());
arr.hello.Add("world",new SortedList());
EN

回答 2

Stack Overflow用户

发布于 2017-06-01 03:56:35

那这个呢。

代码语言:javascript
复制
class NestedSortedList<T> : SortedList<T, NestedSortedList<T>> { }

和测试..

代码语言:javascript
复制
internal class Program
    {
        private static void Main(string[] args)
        {
            var nestedSortedList = new NestedSortedList<string>();
            nestedSortedList.Add("1", new NestedSortedList<string>());
            nestedSortedList.Add("2", new NestedSortedList<string>());
            nestedSortedList.Add("3", new NestedSortedList<string>());

            nestedSortedList["1"].Add("11", new NestedSortedList<string>());
            nestedSortedList["2"].Add("21", new NestedSortedList<string>());
            nestedSortedList["2"].Add("22", new NestedSortedList<string>());

            foreach (var item in nestedSortedList)
            {
                Console.WriteLine(item);
                foreach (var value in item.Value.Values)
                {
                    Console.WriteLine(value);
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
}

这是输出

不创建NestedSortedList类的EDIT>>>

你可以尝试以下方法,尽管你将不得不处理所有这些丑陋的造型。

代码语言:javascript
复制
internal class Program
    {
        private static void Main(string[] args)
        {
            var sortedList = new SortedList();
            sortedList.Add("1", new SortedList());
            sortedList.Add("2", new SortedList());
            sortedList.Add("3", new SortedList());

            ((SortedList)sortedList["1"]).Add("11", new SortedList());
            ((SortedList)sortedList["2"]).Add("21", new SortedList());
            ((SortedList)sortedList["2"]).Add("22", new SortedList());

            foreach (DictionaryEntry dictionaryEntry in sortedList)
            {
                Console.WriteLine("Key: {0}, Value: {1}",dictionaryEntry.Key,dictionaryEntry.Value);
                foreach (DictionaryEntry innerDictionaryEntry in (SortedList)dictionaryEntry.Value)
                {
                    Console.WriteLine("Inner >>> Key: {0}, Value: {1}", innerDictionaryEntry.Key,
                        innerDictionaryEntry.Value);
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
}

输出结果是...

但请当心。由于非泛型SortedList接收类型 object 作为键和值,这意味着您可以存储所需的任何对象,但您始终需要正确地转换它们,以便将它们用作其原始类型

希望这能有所帮助

票数 0
EN

Stack Overflow用户

发布于 2017-06-01 03:56:20

创建一个类

代码语言:javascript
复制
    public class MyList
    {
        string name { get; set;}
        List<MyList> children { get; set; }
    }

代码语言:javascript
复制
    public class MyList
    {

        SortedList<string, MyList> children = new SortedList<string, MyList>();
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44292847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档