日期:2014-05-18 浏览次数:20965 次
var a = ["红色", "蓝色", "白色"]; var b = ["大", "中", "小 "] var c = []; for(var i in a) for(var j in b) c.push([a[i], b[j]]); alert(c);
------解决方案--------------------
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,直接运行,就是你要结果。
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(); } }
------解决方案--------------------
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(); }
------解决方案--------------------