C# . , Length, , . , , . , .
7.10
// Length .
using System;
class LengthDemo
{
static void Main()
{
int[] nums = new int[10];
Console.WriteLine(" nums is " + nums.Length);
// Length nums.
for(int i=0; i < nums.Length; i++)
nums[i] = i * i;
// Length
// nums.
Console.Write(" nums: ");
for(int i=0; i < nums.Length; i++)
Console.Write(nums[i] + " ");
Console.WriteLine();
}
}
.
nums 10
nums: 0 1 4 9 16 25 36 49 64 81
, LengthDemo nums.Length for . , . , , , Length , . Length , .
, , , .
7.11
// Length .
using System;
class LengthDemo3D
{
static void Main()
{
int[,,] nums = new int[10, 5, 6];
Console.WriteLine(" nums " + nums.Length);
}
}
.
nums 300
, Length , ( - 300 (10×5×6) ). Length .
Length , , . Length , .
7.12
// .
using System;
class RevCopy
{
static void Main()
{
int i,j;
int[] nums1 = new int[10];
|
|
int[] nums2 = new int[10];
for(i=0; i < nums1.Length; i++) nums1[i] = i;
Console.Write(" : ");
for(i=0; i < nums2.Length; i++)
Console.Write(nums1[i] + " ");
Console.WriteLine();
// nums1 nums2
// .
if(nums2.Length >= nums1.Length) // ,
// nums2
for(i=0, j=nums1.Length-1; i < nums1.Length; i++, j--)
nums2[j] = nums1[i];
Console.Write(" : ");
for(i=0; i < nums2.Length; i++)
Console.Write(nums2[i] + " ");
Console.WriteLine();
}
}
.
: 0 1 2 3 4 5 6 7 8 9
: 9 8 7 6 5 4 3 2 1 0
Length . - , , . -, for, . , Length, , .