c# 打印一个由泛型列表组成的数组列表
c# printing an array list which is made of generic lists
有一个数组列表,它由泛型列表组成,泛型列表的元素是class个变量。我怎样才能打印整个arrayList?我必须使用数组列表、通用列表和 class。逻辑类似于 Array List[Generic Lists[类]].
namespace temp
{
internal class tempClass
{
public string Name;
public int Number;
}
internal class Program
{
private static void Main(string[] args)
{
string[] Names = { "a", "b", "c", "d", "e", "f","g", "h", "i", "j", "k", "l", "m","n", "o", "p", "q", "r", "s", "t", "u","v", "w", "x", "y", "z", "z2", "z3" };
int[] Number = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,20, 21, 22, 23, 24, 25, 26, 27, 28 };
ArrayList arrayList = new ArrayList();
int counter = 0;
List<tempClass> genericList;
tempClass ClassExample;
for (int i = 0; i < Names.Length;)
{
genericList = new List<tempClass>();
int elementCount = (int)Math.Pow(2, counter);
for (int j = 0; j < elementCount; j++)
{
ClassExample = new tempClass();
ClassExample.Name = Names[i];
ClassExample.Number = Number[i];
genericList.Add(ClassExample);
i++;
if (i == Names.Length) break;
}
arrayList.Add(genericList);
counter++;
}
Console.Read();
}
}
} ```
下面将为您提供一个单维列表,您可以使用 foreach 打印出每个项目。
List<tempClass> tests = arrayList.ToArray()
.Select(obj => (List<tempClass>)obj)
.Aggregate((first, second) => { second.AddRange(first.ToArray()); return second; });
foreach(tempClass test in tests)
{
Console.WriteLine(test.Name);
}
我添加了这个并且成功了,谢谢!
int counter2 = 0;
foreach (List<tempClass> temp in arrayList)
{
foreach (tempClass temp2 in temp)
Console.WriteLine("Name: " + temp2.Name + " Number: " + temp2.Number);
}
有一个数组列表,它由泛型列表组成,泛型列表的元素是class个变量。我怎样才能打印整个arrayList?我必须使用数组列表、通用列表和 class。逻辑类似于 Array List[Generic Lists[类]].
namespace temp
{
internal class tempClass
{
public string Name;
public int Number;
}
internal class Program
{
private static void Main(string[] args)
{
string[] Names = { "a", "b", "c", "d", "e", "f","g", "h", "i", "j", "k", "l", "m","n", "o", "p", "q", "r", "s", "t", "u","v", "w", "x", "y", "z", "z2", "z3" };
int[] Number = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,20, 21, 22, 23, 24, 25, 26, 27, 28 };
ArrayList arrayList = new ArrayList();
int counter = 0;
List<tempClass> genericList;
tempClass ClassExample;
for (int i = 0; i < Names.Length;)
{
genericList = new List<tempClass>();
int elementCount = (int)Math.Pow(2, counter);
for (int j = 0; j < elementCount; j++)
{
ClassExample = new tempClass();
ClassExample.Name = Names[i];
ClassExample.Number = Number[i];
genericList.Add(ClassExample);
i++;
if (i == Names.Length) break;
}
arrayList.Add(genericList);
counter++;
}
Console.Read();
}
}
} ```
下面将为您提供一个单维列表,您可以使用 foreach 打印出每个项目。
List<tempClass> tests = arrayList.ToArray()
.Select(obj => (List<tempClass>)obj)
.Aggregate((first, second) => { second.AddRange(first.ToArray()); return second; });
foreach(tempClass test in tests)
{
Console.WriteLine(test.Name);
}
我添加了这个并且成功了,谢谢!
int counter2 = 0;
foreach (List<tempClass> temp in arrayList)
{
foreach (tempClass temp2 in temp)
Console.WriteLine("Name: " + temp2.Name + " Number: " + temp2.Number);
}