Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

목록의 모든 하위 목록을 인쇄하는 C# 프로그램

<시간/>

먼저 목록을 만드십시오 -

List list = new List();

여기서 문자열은 하위 목록을 찾을 "xyz"입니다. 루핑하는 동안 우리는 또 다른 목록을 선언할 것입니다. 그러면 모든 실제 반복에서 하위 목록이 생성됩니다 -

for (int i = 1; i < str.Length; i++) {
   list.Add(str[i - 1].ToString());
   List newlist = new List();
   for (int j = 0; j < list.Count; j++) {
      string list2 = list[j] + str[i];
      newlist.Add(list2);
   }
   list.AddRange(newlist);
}

다음은 완전한 코드입니다 -

예시

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         string str = "xyz";
         List list = new List();
         for (int i = 1; i < str.Length; i++) {
            list.Add(str[i - 1].ToString());
            List newlist = new List();
            for (int j = 0; j < list.Count; j++) {
               string list2 = list[j] + str[i];
               newlist.Add(list2);
            }
            list.AddRange(newlist);
         }
         list.Add(str[str.Length - 1].ToString());
         list.Sort();
         Console.WriteLine(string.Join(Environment.NewLine, list));
      }
   }
}

출력

x
xy
xyz
xz
y
yz
z