[英]Array of arrays accessing C#
我有四個大小均為2的數組,我想將它們全部放在一個數組中,所以當J為= 1時,它應該使用第一個數組,如果J = 2,則應該訪問第二個數組,依此類推。 。
char[] p1Arr = new char[2];
char[] p2Arr = new char[2];
char[] p3Arr = new char[2];
char[] p4Arr = new char[2];
char[][] pArrays = new char[][] {p1Arr, p2Arr, p3Arr, p4Arr}; //i tried to create this array of the four arrays
//The four arrays values
p1First = p1.Substring(0, 1);
p1Arr[0] = Convert.ToChar(p1First);
p1Second = p1.Substring(1, 1);
p1Arr[1] = Convert.ToChar(p1Second);
p2First = p2.Substring(0, 1);
p2Arr[0] = Convert.ToChar(p2First);
p2Second = p2.Substring(1, 1);
p2Arr[1] = Convert.ToChar(p2Second);
p3First = p3.Substring(0, 1);
p3Arr[0] = Convert.ToChar(p3First);
p3Second = p3.Substring(1, 1);
p3Arr[1] = Convert.ToChar(p3Second);
p4First = p4.Substring(0, 1);
p4Arr[0] = Convert.ToChar(p4First);
p4Second = p4.Substring(1, 1);
p4Arr[1] = Convert.ToChar(p4Second);
//Loop on the four arrays and their two values
for (int j = 0; j < 4; j++)
{
pArrays[][] = pArrays[0][]; //my problem is here, how can i access each array??
//get the first array....fourth.
for (int i = 0; i < 2; i++)
{
//loop on the values of the first array...fourth.
if (i == 0)
{
if (pArrays[j][i] == 0)
{
row = 0;
}
........
}
我無法訪問數組數組中的每個數組,我該如何解決?
您可能可以簡化您的工作
char[][] pArrays =
{
p1.ToCharArray(0, 2),
p2.ToCharArray(0, 2),
p3.ToCharArray(0, 2),
p4.ToCharArray(0, 2)
};
// to looped the arrays
for (int j = 0; j < 4; j++)
{
// to access the original array
var originalArray = pArrays[j];
// to loop the elements
for (int i = 0; i < 2; i++)
{
// to access the elements
// pArrays[j][i]
// originalArray[I]
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.