我有以下方法可以打印出给定数组的所有连续子集。我希望能够将交织在 for 循环中的丑陋 print 语句分离出来并放入一个新函数中。这是可行的吗?
// Example:
// Input: char[] input = new char[] { 'a', 'b', 'c' };
// Output:
// (a) (a b) (a b c)
// (b) (b c)
// (c)
static void PrintContSubArrays(char[] arr)
{
int len = arr.Length;
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for (int i = 0; i < len; i++)
{
for (int j = 1; j <= len; j++)
{
sb2.AppendFormat("(");
for (int k = i; k < j; k++)
{
sb2.AppendFormat("{0} ", arr[k]);
}
sb2.Remove(sb2.Length - 1, 1);
sb2.Append(") ");
if (sb2.Length > 3) sb1.Append(sb2);
sb2.Clear();
}
sb1.Append(System.Environment.NewLine);
}
Console.WriteLine(sb1.ToString());
Console.ReadLine();
}