List
List
List
How can I print the contents of the lists like this:
first item of list A
first item of list B
first item of list C
second item of list A
second item of list B
second item of list C
third item of list A
third item of list B
third item of list C
....
Matthew CochranPosted Jan 25, 2008, 10:45 AM
-Matt
class ProgramMain (string[] args)
List<string> A = new List<string>(){
"1",
"2",
"3"};
{
static void
{
List<string> B = new List<string>(){"W", "X", "Y", "Z"};
List<string> C = new List<string>(){"!", "@"};
new List<String>[] { A, B, C }val
=> Console.WriteLine(val ));
.Collate()
.ForEach(
Console.ReadLine();
}
}
{
public static IEnumerable
{
Int32
i,
length =items.Count();
IEnumerator[]
en = items.Convert(x => x.GetEnumerator()).ToArray();
Boolean[]
// we are finished if there are no more items in the enumerator
(this IEnumerable input, Func f)
(this IEnumerable
items, Action act)
finished = new Boolean[length]; // set to 'false' by default
while (finished.Contains(false))
{
for (i = 0; i < length; i++)
{
finished[i] = ! en[i].MoveNext();
if (!finished[i])
yield return en[i].Current;
}
}
}
public static IEnumerable Convert
{
foreach (T item in input)
yield return f(item);
}
public static void ForEach
{
foreach (T item in items)
act(item);
}
}
AlanPosted Jan 23, 2008, 12:30 PM
OK, this is how I'd suggest doing that:
using System;
using System.Collections.Generic;
class Program A= new List(); B= new List(); C= new List();
{
static void Main()
{
List
A.Add("A1");
A.Add("A2");
List
B.Add("B1");
B.Add("B2");
B.Add("B3");
List
C.Add("C1");
C.Add("C2");
C.Add("C3");
C.Add("C4");
// determine the largest element count
int largest = Math.Max(A.Count, B.Count);
largest = Math.Max(largest, C.Count);
for (int i = 0; i < largest; i++)
{
if (i < A.Count)
Console.WriteLine(A[i]);
if (i < B.Count)
Console.WriteLine(B[i]);
if (i < C.Count)
Console.WriteLine(C[i]);
}
Console.ReadKey();
}
}
To use foreach() you'd have to 'interleave' the three Lists into a single List, so I think the for() statement is the best approach here.
kikiPosted Jan 23, 2008, 12:10 PM
AlanPosted Jan 23, 2008, 12:05 PM
I might be able to propose something but I need to know how you'd want to deal with the 'excess' elements.
Suppose that A had 2 elements, B had 3 elements and C had 4 elements. How would you want to print that out?
Something like this may be:
A1
B1
C1
A2
B2
C2
B3
C3
C4
kikiPosted Jan 23, 2008, 11:58 AM
I thought foreach statement was the solution to my problem because I dont need to know the sizes of the lists and because it's quicker in execution.
AlanPosted Jan 23, 2008, 11:50 AM
Assuming that all 3 lists have the same number of elements, you could do something like this:
using System;
using System.Collections.Generic;
class Program A= new List(); B= new List(); C= new List();
{
static void Main()
{
List
A.Add("A1");
A.Add("A2");
A.Add("A3");
List
B.Add("B1");
B.Add("B2");
B.Add("B3");
List
C.Add("C1");
C.Add("C2");
C.Add("C3");
for (int i = 0; i < A.Count; i++)
{
Console.WriteLine(A[i]);
Console.WriteLine(B[i]);
Console.WriteLine(C[i]);
}
Console.ReadKey();
}
}
I can't see how you could use foreach() here as you need to know the index of the element to be printed.