实现动态数组的一种方法
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Array
... {
class Program
...{
static void PrintArr(int ArrLength)
...{ //给数组赋值并输出
int [] arr = new int [ArrLength];
for (int i = 0; i < arr.Length; i++)
arr[i] = i;
Console.WriteLine("Array's value is");
for (int i = 0; i < arr.Length; i++)
Console.WriteLine("arr[{0}]={1}",i,arr[i]);
}
static void Main(string[] args)
...{
int i = 1;
while (i > 0)
...{ //输入数组长度
Console.WriteLine("Input the Length of Array");
i = Int32.Parse(Console.ReadLine());
PrintArr(i);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Array
... {
class Program
...{
static void PrintArr(int ArrLength)
...{ //给数组赋值并输出
int [] arr = new int [ArrLength];
for (int i = 0; i < arr.Length; i++)
arr[i] = i;
Console.WriteLine("Array's value is");
for (int i = 0; i < arr.Length; i++)
Console.WriteLine("arr[{0}]={1}",i,arr[i]);
}
static void Main(string[] args)
...{
int i = 1;
while (i > 0)
...{ //输入数组长度
Console.WriteLine("Input the Length of Array");
i = Int32.Parse(Console.ReadLine());
PrintArr(i);
}
}
}
}
也可以在新定义的类只写出来方法,在MAIN函数中来调用其用法如下:
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Array
... {
class SetArr
...{
public void PrintArr(int ArrLength)
...{
//给数组赋值并输出
int[] arr = new int[ArrLength];
for (int i = 0; i < arr.Length; i++)
arr[i] = i;
Console.WriteLine("Array's value is");
for (int i = 0; i < arr.Length; i++)
Console.WriteLine("arr[{0}]={1}", i, arr[i]);
}
}
class Program
...{
static void Main(string[] args)
...{
SetArr arr = new SetArr();
int i = 1;
while (i > 0)
...{ //输入数组长度
Console.WriteLine("Input the Length of Array");
i = Int32.Parse(Console.ReadLine());
arr.PrintArr(i);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Array
... {
class SetArr
...{
public void PrintArr(int ArrLength)
...{
//给数组赋值并输出
int[] arr = new int[ArrLength];
for (int i = 0; i < arr.Length; i++)
arr[i] = i;
Console.WriteLine("Array's value is");
for (int i = 0; i < arr.Length; i++)
Console.WriteLine("arr[{0}]={1}", i, arr[i]);
}
}
class Program
...{
static void Main(string[] args)
...{
SetArr arr = new SetArr();
int i = 1;
while (i > 0)
...{ //输入数组长度
Console.WriteLine("Input the Length of Array");
i = Int32.Parse(Console.ReadLine());
arr.PrintArr(i);
}
}
}
}