どのように繰り返し処理を実行し、ギザギザ配列?
-
09-06-2019 - |
質問
これは、運転んです。なぜなのか?
Dim arr(3, 3) As Integer For y As Integer = 0 To arr.GetLength(0) - 1 For x As Integer = 0 To arr.GetLength(y) - 1 arr(y, x) = y + x Next Next
また、どの場合、配列のように見えるこのょうか?
{ {1, 2, 3},
{4, 5, 6, 7, 8, 9, 9, 9},
{5, 4, 3, 2}
}
解決
ですが、何が本当に必要である"ギザギザ配列"となります。ることができましては、"配列を含むその他の配列の異なる長さ".
Dim arr As Integer()() = {New Integer() {1, 2, 3}, New Integer() {4, 5, 6, 7, 8, 9, 9, 9}, New Integer() {5, 4, 3, 2}}
For x = 0 To arr.GetUpperBound(0)
Console.WriteLine("Row " & x & " has " & arr(x).GetUpperBound(0) & " columns")
For y = 0 To arr(x).GetUpperBound(0)
Console.WriteLine("(" & x & "," & y & ") = " & arr(x)(y))
Next
Next
出力:
Row 0 has 2 columns
(0,0) = 1
(0,1) = 2
(0,2) = 3
Row 1 has 7 columns
(1,0) = 4
(1,1) = 5
(1,2) = 6
(1,3) = 7
(1,4) = 8
(1,5) = 9
(1,6) = 9
(1,7) = 9
Row 2 has 3 columns
(2,0) = 5
(2,1) = 4
(2,2) = 3
(2,3) = 2
他のヒント
のではありません'2'または'3'側面がある。きます。GetLength(1)です。GetLength(y)
また:にVB.Net 配列の宣言のがちょっとでは異なります。の添え字を指定する申告書の最後のインデックスは、品目の個数を作成したようであり、C#とC++.その配列が0のインデックスのようであり、C#とC++ではなく、1変動利付債物価連動などVB6.ということができVB.Net 異なる言語の配列に捉え間違っているのは、いずれの言語です。にVB.Net, Dim着(3,3)として整数 実際に作成さ 4x4 配列に格納します。
arr.GetLength(y)
すべき
arr.GetLength(1)
ものになっているのでしょうか?した配列のようにこの
{ {1, 2, 3},
{4, 5, 6, 7, 8, 9, 9, 9},
{5, 4, 3, 2}
}
どうGetLength(1)いまだ知らの長さがそれぞれの行?
基本的にはだいたいです。 とその応用についての要素数に指定された行為.
Dim arr(3, 3) As Integer
Dim y As Integer
Dim x As Integer
For x = 0 To arr.Rank - 1
For y = 0 To arr.GetLength(x) - 2
arr(x, y) = x + y
Next
Next
上記のコードは勤務しました。
編集、コード感じ汚れたものです。ったかはしようとしているが達成?
このコードja C#では全ての組み合わせの項目にギザギザ配列:
static void Main(string[] args)
{
bool exit = false;
int[] indices = new int[3] { 0, 0, 0 };
string[][] vectores = new string[3][];
vectores[0] = new string[] { "A", "B", "C" };
vectores[1] = new string[] { "A", "B" };
vectores[2] = new string[] { "B", "D", "E", "F" };
string[] item;
int[] tamaños = new int[3]{vectores[0].GetUpperBound(0),
vectores[1].GetUpperBound(0),
vectores[2].GetUpperBound(0)};
while (!exit)
{
item = new string[]{ vectores[0][indices[0]],
vectores[1][indices[1]],
vectores[2][indices[2]]};
Console.WriteLine("[{0},{1},{2}]={3}{4}{5}", indices[0], indices[1], indices[2], item[0], item[1], item[2]);
GetVector(tamaños, ref indices, ref exit);
}
Console.ReadKey();
}
public static void GetVector(int[] tamaños, ref int[] indices, ref bool exit)
{
for (int i = tamaños.GetUpperBound(0); i >= 0; i--)
{
if (tamaños[i] > indices[i])
{
indices[i]++;
break;
}
else
{
//ULTIMO ITEM EN EL ARRAY, VALIDAR LAS OTRAS DIMENSIONES SI YA ESTA EN EL ULTIMO ITEM
if (!ValidateIndexes(tamaños, indices))
indices[i] = 0;
else
{
exit = true;
break;
}
}
}
}
public static bool ValidateIndexes(int[] tamaños, int[] indices)
{
for (int i = 0; i < tamaños.Length; i++)
{
if (tamaños[i] != indices[i])
return false;
}
return true;
}
の出力のように見え [0,0,0]=AAB [0,0,1]=AAD [0,0,2]=オーストラリア南極探検隊 [0,0,3]=AAF [0,1,0]=ABB [0,1,1]=ABD [0,1,2]=阿部 [0,1,3]=ABF [1,0,0]=バブ [1,0,1]=悪 [1,0,2]=BAE [1,0,3]=BAF [1,1,0]=BBB [1,1,1]=BBD [1,1,2]=BBE [1,1,3]=BBF [2,0,0]=転 [2,0,1]=CAD [2,0,2]=CAE [2,0,3]=カフェ [2,1,0]=CBB [2,1,1]=CBD [2,1,2]=C [2,1,3]=CBF
お宣言: DIM arr(3,3) As Integer
allreadyを指定することが3つの要素に任意の指定された行は4日ませんので必ず約VB)
きみ:
Dim arr(3) as Integer()
してくださいきん:
arr(n).Length
の長さの連続。
いてはちょっとした錆びにVB6ったVB.NETこんに"ギ'配列の型になります。チェックにmsdn文書multidimensioned arrays.