简体   繁体   English

嵌套SortedList

[英]Nested SortedList

I am a novice at C# and come from a PHP background where multidimensional arrays are a walk in the park. 我是C#的新手,来自PHP背景,多维数组在公园里散步。

How can one create a nested/multidimensional array with a SortedList in C#? 如何使用C#中的SortedList创建嵌套/多维数组? I believe from reading that the SortedList is probably the most comparative in terms of PHP functionality. 从阅读中我相信,就PHP功能而言,SortedList可能是最具有可比性的。 I've tried the following but it throws errors: 我尝试了以下操作,但会引发错误:

SortedList arr = new SortedList();
arr.Add("hello",new SortedList());
arr.hello.Add("world",new SortedList());

What about this. 那这个呢。

class NestedSortedList<T> : SortedList<T, NestedSortedList<T>> { }

And testing.. 并测试..

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();
        }
}

This is the output 这是输出

在此处输入图片说明

EDIT>>> Without creating NestedSortedList class 编辑>>>而不创建NestedSortedList类

You may try the following, although you will have to deal with all those ugly casts. 您可以尝试以下方法,尽管您将不得不处理所有这些丑陋的演员。

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();
        }
}

And the output... 和输出...

在此处输入图片说明

But please beware . 但是请当心 Since non generic SortedList receives type object as key and value, this mean you could store whichever object you want but you will always need to cast them correctly in order to use them as its original type is intended to be used 由于非通用SortedList接收类型对象作为键和值,因此这意味着您可以存储所需的任何对象,但是始终需要正确地强制转换它们才能使用它们,因为要使用其原始类型

Hope this helps 希望这可以帮助

Make a class 上课

    public class MyList
    {
        string name { get; set;}
        List<MyList> children { get; set; }
    }

or 要么

    public class MyList
    {

        SortedList<string, MyList> children = new SortedList<string, MyList>();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM