使用c#分别生成 4维,5维,6维数组(分别为int类型,double类型,string类型)(每个花括号里都有随机的数值) 并且遍历数组
时间: 2023-06-19 10:03:39 浏览: 125
以下是分别生成 4维、5维、6维数组的示例代码:
```csharp
// 生成 int 类型的 4 维数组
int[,,,] intArray4D = new int[2, 3, 4, 5]
{
{
{
{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, 29, 30},
{31, 32, 33, 34, 35},
{36, 37, 38, 39, 40}
},
{
{41, 42, 43, 44, 45},
{46, 47, 48, 49, 50},
{51, 52, 53, 54, 55},
{56, 57, 58, 59, 60}
}
},
{
{
{61, 62, 63, 64, 65},
{66, 67, 68, 69, 70},
{71, 72, 73, 74, 75},
{76, 77, 78, 79, 80}
},
{
{81, 82, 83, 84, 85},
{86, 87, 88, 89, 90},
{91, 92, 93, 94, 95},
{96, 97, 98, 99, 100}
},
{
{101, 102, 103, 104, 105},
{106, 107, 108, 109, 110},
{111, 112, 113, 114, 115},
{116, 117, 118, 119, 120}
}
}
};
// 遍历 int 类型的 4 维数组
for (int i = 0; i < intArray4D.GetLength(0); i++)
{
for (int j = 0; j < intArray4D.GetLength(1); j++)
{
for (int k = 0; k < intArray4D.GetLength(2); k++)
{
for (int l = 0; l < intArray4D.GetLength(3); l++)
{
Console.WriteLine("intArray4D[{0}, {1}, {2}, {3}] = {4}", i, j, k, l, intArray4D[i, j, k, l]);
}
}
}
}
// 生成 double 类型的 5 维数组
double[,,,,] doubleArray5D = new double[2, 3, 4, 5, 2]
{
{
{
{
{1.1, 1.2},
{2.1, 2.2},
{3.1, 3.2},
{4.1, 4.2},
{5.1, 5.2}
},
{
{6.1, 6.2},
{7.1, 7.2},
{8.1, 8.2},
{9.1, 9.2},
{10.1, 10.2}
},
{
{11.1, 11.2},
{12.1, 12.2},
{13.1, 13.2},
{14.1, 14.2},
{15.1, 15.2}
},
{
{16.1, 16.2},
{17.1, 17.2},
{18.1, 18.2},
{19.1, 19.2},
{20.1, 20.2}
}
},
{
{
{21.1, 21.2},
{22.1, 22.2},
{23.1, 23.2},
{24.1, 24.2},
{25.1, 25.2}
},
{
{26.1, 26.2},
{27.1, 27.2},
{28.1, 28.2},
{29.1, 29.2},
{30.1, 30.2}
},
{
{31.1, 31.2},
{32.1, 32.2},
{33.1, 33.2},
{34.1, 34.2},
{35.1, 35.2}
},
{
{36.1, 36.2},
{37.1, 37.2},
{38.1, 38.2},
{39.1, 39.2},
{40.1, 40.2}
}
},
{
{
{41.1, 41.2},
{42.1, 42.2},
{43.1, 43.2},
{44.1, 44.2},
{45.1, 45.2}
},
{
{46.1, 46.2},
{47.1, 47.2},
{48.1, 48.2},
{49.1, 49.2},
{50.1, 50.2}
},
{
{51.1, 51.2},
{52.1, 52.2},
{53.1, 53.2},
{54.1, 54.2},
{55.1, 55.2}
},
{
{56.1, 56.2},
{57.1, 57.2},
{58.1, 58.2},
{59.1, 59.2},
{60.1, 60.2}
}
}
},
{
{
{
{61.1, 61.2},
{62.1, 62.2},
{63.1, 63.2},
{64.1, 64.2},
{65.1, 65.2}
},
{
{66.1, 66.2},
{67.1, 67.2},
{68.1, 68.2},
{69.1, 69.2},
{70.1, 70.2}
},
{
{71.1, 71.2},
{72.1, 72.2},
{73.1, 73.2},
{74.1, 74.2},
{75.1, 75.2}
},
{
{76.1, 76.2},
{77.1, 77.2},
{78.1, 78.2},
{79.1, 79.2},
{80.1, 80.2}
}
},
{
{
{81.1, 81.2},
{82.1, 82.2},
{83.1, 83.2},
{84.1, 84.2},
{85.1, 85.2}
},
{
{86.1, 86.2},
{87.1, 87.2},
{88.1, 88.2},
{89.1, 89.2},
{90.1, 90.2}
},
{
{91.1, 91.2},
{92.1, 92.2},
{93.1, 93.2},
{94.1, 94.2},
{95.1, 95.2}
},
{
{96.1, 96.2},
{97.1, 97.2},
{98.1, 98.2},
{99.1, 99.2},
{100.1, 100.2}
}
},
{
{
{101.1, 101.2},
{102.1, 102.2},
{103.1, 103.2},
{104.1, 104.2},
{105.1, 105.2}
},
{
{106.1, 106.2},
{107.1, 107.2},
{108.1, 108.2},
{109.1, 109.2},
{110.1, 110.2}
},
{
{111.1, 111.2},
{112.1, 112.2},
{113.1, 113.2},
{114.1, 114.2},
{115.1, 115.2}
},
{
{116.1, 116.2},
{117.1, 117.2},
{118.1, 118.2},
{119.1, 119.2},
{120.1, 120.2}
}
}
}
};
// 遍历 double 类型的 5 维数组
for (int i = 0; i < doubleArray5D.GetLength(0); i++)
{
for (int j = 0; j < doubleArray5D.GetLength(1); j++)
{
for (int k = 0; k < doubleArray5D.GetLength(2); k++)
{
for (int l = 0; l < doubleArray5D.GetLength(3); l++)
{
for (int m = 0; m < doubleArray5D.GetLength(4); m++)
{
Console.WriteLine("doubleArray5D[{0}, {1}, {2}, {3}, {4}] = {5}", i, j, k, l, m, doubleArray5D[i, j, k, l, m]);
}
}
}
}
}
// 生成 string 类型的 6 维数组
string[,,,,,] stringArray6D = new string[2, 3, 4, 5, 2, 2]
{
{
{
{
{
{"a1", "a2"},
{"b1", "b2"},
{"c1", "c2"},
{"d1", "d2"},
{"e1", "e2"}
},
{
{"f1", "f2"},
{"g1", "g2"},
{"h1", "h2"},
{"i1", "i2"},
{"j1", "j2"}
},
{
{"k1", "k2"},
{"l1", "l2"},
{"m1", "m2"},
{"n1", "n2"},
{"o1", "o2"}
},
{
{"p1", "p2"},
{"q1", "q2"},
{"r1", "r2"},
{"s1", "s2"},
{"t1", "t2"}
}
},
{
{
{"u1", "u2"},
{"v1", "v2"},
{"w1", "w2"},
{"x1", "x2"},
{"y1", "y2"}
},
{
{"z1", "z2"},
{"A1", "A2"},
{"B1", "B2"},
{"C1", "C2"},
{"D1", "D2"}
},
{
{"E1", "E2"},
{"F1", "F2"},
{"G1", "G2"},
{"H1", "H2"},
{"I1", "I2"}
},
{
{"J1", "J2"},
{"K1", "K2"},
{"L1", "L2"},
{"M1", "M2"},
{"N1", "N2"}
}
},
{
{
{"O1", "O2"},
{"P1", "P2"},
{"Q1", "Q2"},
{"R1", "R2"},
{"S1", "S2"}
},
{
{"T1", "T2"},
{"U1", "U2"},
{"V1", "V2"},
{"W1", "W2"},
{"X1", "X2"}
},
{
{"Y1", "Y2"},
{"Z1", "Z2"},
{"1a", "1b"},
{"2c", "2d"},
{"3e", "3f"}
},
{
{"4g", "4h"},
{"5i", "5j"},
{"6k", "6l"},
{"7m", "7n"},
{"8o", "8p"}
}
}
},
{
{
{
{"9q", "9r"},
{"0s", "0t"},
{"1u", "1v"},
{"2w", "2x"},
{"3y", "3z"}
},
{
{"4A", "4B"},
{"5C", "5D"},
{"6E", "6F"},
{"7G", "7H"},
{"8I", "8J"}
},
{
{"9K", "9L"},
{"0M", "0N"},
{"1O", "1P"},
{"2Q", "2R"},
{"3S", "3T"}
},
{
{"4U", "4V"},
{"5W", "5X"},
{"6Y", "6Z"},
{"7a", "7b"},
{"8c", "8d"}
}
},
{
{
{"9e", "9f"},
{"0g", "0h"},
{"1i", "1j"},
{"2k", "2l"},
{"3m", "3n"}
},
{
{"4o", "4p"},
{"5q", "5r"},
{"6s", "6t"},
{"7u", "7v"},
{"8w", "8x"}
},
{
{"9y", "9z"},
{"0A", "0B"},
{"1C", "1D"},
{"2E", "2F"},
{"3G", "3H"}
},
{
{"4I", "4J"},
{"5K", "5L"},
{"6M", "6N"},
{"7O", "7P"},
{"8Q", "8R"}
}
},
{
{
{"9S", "9T"},
{"0U", "0V"},
{"1W", "1X"},
{"2Y", "2Z"},
{"3a", "3b"}
},
{
{"4c", "4d"},
{"5e", "5f"},
{"6g", "6h"},
{"7i", "7j"},
{"8k", "8l"}
},
{
{"9m", "9n"},
{"0o", "0p"},
{"1q", "1r"},
{"2s", "2t"},
{"3u", "3v"}
},
{
{"4w", "4x"},
{"5y", "5z"},
{"6A", "6B"},
{"7C", "7D"},
{"8E", "8F"}
}
}
}
},
{
{
{
{
{"9G", "9H"},
{"0I", "0J"},
{"1K", "1L"},
{"2M", "2N"},
{"3O", "3P"}
},
{
{"4Q", "4R"},
{"5S", "5T"},
{"6U", "6V"},
{"7W", "7X"},
{"8Y", "8Z"}
},
{
{"9a", "9b"},
{"0c", "0d"},
{"1e", "1f"},
{"2g", "2h"},
{"3i", "3j"}
},
{
{"4k", "4l"},
{"5m", "5n"},
{"6o", "6p"},
{"7q", "7r"},
{"8s", "8t"}
}
},
{
{
{"9u", "9v"},
{"0w", "0x"},
{"1y", "1z"},
{"2A", "2B"},
{"3C", "3D"}
},
{
{"4E", "4F"},
{"5G", "5H"},
{"6I", "6J"},
{"7K", "7L"},
{"8M", "8N"}
},
{
{"9O", "9P"},
{"0Q", "0R"},
{"1S", "1T"},
{"2U", "2V"},
{"3W", "3X"}
},
{
{"4Y", "4Z"},
{"5a", "5b"},
{"6c", "6d"},
{"7e", "7f"},
{"8g", "8h"}
}
},
{
阅读全文