在循环中获取许多值

时间:2015-01-30 05:01:39

标签: c# arrays for-loop

我有一个4阵列:

var array1[] = {1,2,3,4}
var array2[] = {a,b,c,d}
var array3[] = {A,B,C,D}
var array4[] = {10,20,30,40}

现在,我想在1个循环中从这4个数组中获取4个值,那么怎么做呢,就像1个循环的输出一样:

"1,a,A,10"

3 个答案:

答案 0 :(得分:3)

我想你想要:

var array2[] = {'a','b','c','d'};
var array3[] = {'A','B','C','D'};

由你的第二和第三阵列

无论如何,您可以按照评论

中的说明循环浏览它们
for(int i=0; i<4; i++)
{
   Console.WriteLine("\"{0},{1},{2},{3}\"", array1[i], array2[i], array3[i], array4[i]);
}

答案 1 :(得分:0)

for(int i=0; i<4; i++) 
{
 String a = String.Format("\"{0},{1},{2},{3}\"", array1[i], array2[i], array3[i], array4[i]);

 // Do what you want with the value here
}

答案 2 :(得分:0)

another wayput all arrays into one array and loop through it

    static void Main(string[] args)
    {
        Object[] array1 = { 1, 2, 3, 4 };
        Object[] array2 = { 'a', 'b', 'c', 'd' };
        Object[] array3 = { 'A', 'B', 'C', 'D' };
        Object[] array4 = { 10, 20, 30, 40 };
        var arrays = new Object[][] { array1, array2, array3, array4 };
        string str = "";
        for (int i = 0; i < arrays.Length; i++)
        {
            str += arrays[i][0].ToString()+',';
        }
        str = str.Remove(str.Length - 1);
        Console.WriteLine(str);
        Console.ReadLine();
    }

输出: 1,a,A,10