日期:2014-05-18  浏览次数:20951 次

数组交叉循环搭配的排列组合问题
有多个数组.如何组成一个二维数组
如:
数组一:红色,蓝色,白色
数组二:大,中,小
最终组成的数组为:{红色,大}{红色,中}{红色,小}{蓝色,大}{蓝色,中}{蓝色,小}{白色,大}{白色,中}{白色,小}

条件:一维数组的个数不确定,有可能有三个,有可能有四个等.

请教高手如何实现

------解决方案--------------------
JScript code
var a = ["红色", "蓝色", "白色"];
var b = ["大", "中", "小 "]
var c = [];
for(var i in a)
    for(var j in b)
        c.push([a[i], b[j]]);

alert(c);

------解决方案--------------------
C# code
string[] arr1 = new string[] { "红色", "蓝色", "白色" };
            string[] arr2 = new string[] { "大", "中", "小","超大" };

            string[][] arr3 = new string[arr1.Length*arr2.Length][];

            for (int i = 0; i < arr1.Length; i++)
            {
                for (int j = 0; j < arr2.Length; j++)
                {
                    string[] arr = new string[] { arr1[i], arr2[j] };
                    arr3[i + arr1.Length*j] = arr;
                }
            }

------解决方案--------------------
下面代码,加上必要using,直接运行,就是你要结果。

C# code

class Program
    {
        static void Main(string[] args)
        {
            testArray();
        }
        private static void testArray()
        {
            object[] a = {"红色", "蓝色", "白色","黑色"};
            object[] b = {"大", "中", "小"};
            int n = a.Length*b.Length;
            object[][] c = new object[n][];
            for (int i = 0; i < n; i++)
            {
                c[i] = new object[2];
                c[i][0] = a[i / b.Length];
                c[i][1] = b[i % b.Length];
            }

            System.Console.WriteLine("a = " + Array2String(a));
            System.Console.WriteLine("b = " + Array2String(b));
            System.Console.WriteLine("c = " + Array2String(c));
            System.Console.ReadLine();
        }
        private static string Array2String(object[] arr){
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            for (int i = 0;i<arr.Length;i++)
            {
                object o = arr[i];
                if (o is Array)
                    sb.Append(Array2String((object[])o));
                else
                    sb.Append(o);
                if (i < arr.Length - 1)
                {
                    sb.Append(",");
                }
            }
            sb.Append("}");
            return sb.ToString();
        }
}

------解决方案--------------------
C# code

    class testArray
    {
        private string[] str1 = { "红", "黄", "蓝" };
        private string[] str2 = { "大", "中", "小" };
        private string[,] str3 = null;
        public void newArray()
        {
            int m = str1.Length;
            int n = str2.Length;
            str3 = new string[m, n];
            for (int i = 0; i < str1.Length; i++)
            {
                for (int j = 0; j < str2.Length; j++)
                {
                    str3[i,j] = str1[i] + str2[j];
                }
            }
        }

        public void printNewArray()
        {
            for (int i = 0; i < str1.Length; i++)
            {
                for (int j = 0; j < str2.Length; j++)
                {
                    System.Console.WriteLine(str3[i,j]);
                }
            }
        }
        static void Main(string[] args)
        {
            testArray ta = new testArray();
            ta.newArray();
            ta.printNewArray();
        }

------解决方案--------------------