我有这些字符串数组:
string[] BayReplyArray1 = new string[30];
string[] BayReplyArray2 = new string[30];
string[] BayReplyArray3 = new string[30];
... up to 10
我想在这样的循环中访问它们:
for (int i = 1; i < 11; i++)
{
BayReplyArray[i] = "test";
}
它说BayReplyArray在当前上下文中不存在。我可以看出为什么它很困惑,但我怎么能做到这一点?
答案 0 :(得分:6)
对此的一个简单解决方案是多维数组。
string[,] BayReplyArrays = new string[10,30]
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 30; j++)
{
BayReplyArrays[i,j] = "test";
}
}
这是MSDN多维数组指南:{{3}}
答案 1 :(得分:1)
您可以使用以下内容:
const int NumberOfArrays = 10;
const int NumberOfValuesInEachArray = 30;
// array of arrays (could also use a List)
var AllBayReplyArrays = new string[NumberOfArrays][];
// create each nested array
for (var i = 0; i < NumberOfArrays; i++)
{
AllBayReplyArrays[i] = new string[NumberOfValuesInEachArray];
}
// set values in each array
for (var i = 0; i < NumberOfArrays; i++)
{
for (var j = 0; j < NumberOfValuesInEachArray; j++)
{
// you can write whatever values you like here, I have added the indices to be able to validate the output
AllBayReplyArrays[i][j] = String.Format("Test {0}-{1}", i, j);
}
}
// check the values:
Console.WriteLine("The 5th value of the 1st array should be Test 0-4 (for zero based indexes), it is: {0}", AllBayReplyArrays[0][4]);
Console.Read(); //to pause execution
输出结果为:
第一个数组的第5个值应为Test 0-4(基于零 索引),它是:测试0-4
在一个班轮中获得相同结果的另一种方法是:
var AllBayReplyArrays = Enumerable.Range(0,9).Select(i => Enumerable.Range(0,29).Select(j => String.Format("Test {0}-{1}", i, j)).ToArray()).ToArray();
您可以在第一个解决方案的底部使用相同的两个测试行来检查它。
答案 2 :(得分:0)
除了其他答案,你可以创建一个数组列表然后循环它。
string[] arrString1 = new string[30];
string[] arrString2 = new string[30];
string[] arrString3 = new string[30];
List<string[]> ListOfArrays = new List<string[]>{ arrString1, arrString2, arrString3};
for (int i = 0; i < ListOfArrays.Count; ++i)
{
string[] CurrentList = ListOfArrays[i];
for (int i2 = 0; i2 < CurrentList.Count(); ++i2)
{
CurrentList[i2] = "Test";
}
}
答案 3 :(得分:0)
感谢所有评论,他们每个人都以某种方式激励我。结果最终使用了锯齿状阵列。
// declare jagged array with 3 rows
string[][] jaggedArray = new string[3][];
// create new arrays in the jagged array and give them a size
jaggedArray[0] = new string[5];
jaggedArray[1] = new string[5];
jaggedArray[2] = new string[5];
// this assigns values to the first row
jaggedArray[0][0] = "test1";
jaggedArray[0][1] = " test2 ";
jaggedArray[0][2] = "test3";
jaggedArray[0][3] = "test4";
jaggedArray[0][4] = "test5";
// this assigns values to the second row
for (int i = 0; i < jaggedArray[1].Length; i++)
{
jaggedArray[1][i] = "tt" + i;
}
// this assigns values to the third row
//jaggedArray[2] = new string[3] { "k1", "k2", "k3" };
string StrTest = "the:quick:brown: fox ";
char[] delimiters = new char[] { 'u', ':' };
jaggedArray[2] = StrTest.Split(delimiters);
// Print out all elements in the jagged array.
for (int i = 0; i < jaggedArray.Length; i++)
{
string[] innerArray = jaggedArray[i];
for (int j = 0; j < innerArray.Length; j++)
{
// this trims whitespace from all elements in the jagged array
innerArray[j] = innerArray[j].Trim();
Console.Write(innerArray[j] + " ");
}
// this fires after each row in the jagged array
Console.WriteLine(i);
}