程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 一個關於遞歸的示例

一個關於遞歸的示例

編輯:C#入門知識

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


摘自 wangganggang90的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved