一个关于递归的示例

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

            }
        }
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值