首先,设置数组-
int[] p = new int[] {55, 66, 88, 99, 111, 122, 133};
现在,让我们说您需要在位置1处设置一个元素-
p[2] = 77;
让我们看看编译代码-
using System; namespace Program { public class Demo { public static void Main(string[] args) { int[] p = new int[] {55, 66, 88, 99, 111, 122, 133}; int j; Console.WriteLine("Initial Array......"); for (j = 0; j < p.Length; j++ ) { Console.WriteLine("arr[j] = {0}", p[j]); } //在索引2处设置新元素 p[2] = 77; Console.WriteLine("New Array......"); for (j = 0; j < p.Length; j++ ) { Console.WriteLine("arr[j] = {0}", p[j]); } } } }
输出结果
Initial Array...... arr[j] = 55 arr[j] = 66 arr[j] = 88 arr[j] = 99 arr[j] = 111 arr[j] = 122 arr[j] = 133 New Array...... arr[j] = 55 arr[j] = 66 arr[j] = 77 arr[j] = 99 arr[j] = 111 arr[j] = 122 arr[j] = 133