一个关于递归的示例

来源:互联网 发布:龙芯linux计算机编程 编辑:程序博客网 时间:2024/06/03 13:09

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace SortApplication
{
    public class getList
    {
        public int id;
        public int parentId;
        public string name;

        public getList(int id, int parentId, string name)
        {
            this.id = id;
            this.parentId = parentId;
            this.name = name;
        }
    }

    class Program
    {
        private static IList<getList> GetList(getList[] myList, int pid)
        {
            int count = myList.Length;
            IList<getList> al = new List<getList>();

                for (int i = 0; i < count; i++)
                {
                    if (myList[i].parentId == pid)
                    {
                        // Console.WriteLine(”上面测试 : ” + pid);
                        al.Add(myList[i]);
                    }
                }
            return al;
        }

        private static void SortList(getList[] myList, IList<getList> Al, ref getList[] listCopy)
        {
            if (listCopy[0] == null)
            {
                Al.Add(myList[0]);
            }
            try
            {
                foreach (getList lis in Al)
                {
                    int i;
                    for (i = 0; i < listCopy.Length; i++)
                    {
                        if (listCopy[i] == null)
                        {
                            break;
                        }
                    }
                    // Console.WriteLine("///" + i + "//////");
                    listCopy[i] = (getList)lis;
                    // Console.WriteLine(f);
                    Al = GetList(myList, lis.id);
                    SortList(myList, Al, ref listCopy);
                  
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
        static void Main(string[] args)
        {
            getList[] ourList = new getList[] {
                new getList(1, 0, "a"),
                new getList(2, 1, "b"),
                new getList(3, 1, "c"),
                new getList(4, 2, "d"),
                new getList(5, 4, "e"),
                new getList(7, 3, "aaa"),
                new getList(6, 5, "f")
            };
                  
            string BlankBase = new string(' ', 8);
            Console.WriteLine("排序以前的对象");
            for (int i = 0; i < ourList.Length; i++)
            {
                Console.WriteLine(ourList[i].id + BlankBase + ourList[i].parentId + BlankBase + ourList[i].name);
            }
            getList[] listCopy = new getList[ourList.Length];

            IList<getList> Alist = new List<getList>();
            SortList(ourList, Alist, ref listCopy);

            Console.WriteLine("\r\n\r\n排序以后的对象");

            for (int i = 0; i < listCopy.Length; i++)
            {
                try
                {
                    Console.WriteLine(listCopy[i].id + BlankBase + listCopy[i].parentId + BlankBase + listCopy[i].name);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

            }
        }
    }
}

 

原创粉丝点击