配列

このヘルプでは、COBOL、C#、VB.NET での配列の使い方について説明します。

COBOL の配列

01 nums binary-long occurs 3 values 1, 2, 3.
*> Can also do:
set content of nums to (1 2 3)

*> 5 is the size of the array
01 names string occurs 5.
*> Can also do:
01 names string occurs any.
set size of names to 5
set names(1) to "David"  *> first element indexed as 1
set names(6) to "Bobby"  *> throws System.IndexOutOfRangeException

*> COBOL cannot resize an array - use copy
01 names2 string occurs 7.
invoke type Array::Copy(names, names2, names::Length)
*> or else:
invoke names::CopyTo(names2, 0)
*> or else:
invoke type Array::Resize(names, 7)

01 twoD float-short occurs any, any.
set size of twoD to rows, cols

01 jagged binary-long occurs any, occurs any.
set size of jagged to 3
set size of jagged(1) to 5
set jagged(1 5) to 5

C# の配列

int[] nums = {1, 2, 3};
for (int i = 0; i < nums.Length; i++)
{
   Console.WriteLine(nums[i]);
}

// 5 is the size of the array
string[] names = new string[5];
names[0] = "David";
names[5] = "Bobby";   // Throws System.IndexOutOfRangeException


// C# can't dynamically resize an array.  Just copy into new array.
string[] names2 = new string[7];
// or names.CopyTo(names2, 0);
Array.Copy(names, names2, names.Length);



float[,] twoD = new float[rows, cols];
twoD[2,0] = 4.5f;

int[][] jagged = new int[3][] {
  new int[5], new int[2], new int[3] };
jagged[0][4] = 5;

VB.NET の配列

Dim nums() As Integer = {1, 2, 3}
For i As Integer = 0 To nums.Length - 1
   Console.WriteLine(nums(i))
Next

' 4 is the index of the last element, so it holds 5 elements
Dim names(4) As String
names(0) = "David"
names(5) = "Bobby"  ' Throws System.IndexOutOfRangeException

' Resize the array, keeping the existing values (Preserve is optional)
' Note however, that this produces a new copy of the
' array - it is not an in-place resize!

ReDim Preserve names(6)




Dim twoD(rows-1, cols-1) As Single
twoD(2, 0) = 4.5

Dim jagged()() As Integer = { _
  New Integer(4) {}, New Integer(1) {}, New Integer(2) {} }
jagged(0)(4) = 5

これらの例の一部は、ハーディング大学コンピュータ サイエンス学部の Frank McCown 博士が作成したもので、クリエイティブ コモンズ ライセンスに基づいて使用が許可されています。