C#中的数组。
C#的数组和我之前使用的PHP的数组完全不一样。
C#的数组是需要实例化的,实例化的方式有两种
1:直接赋值。
2:使用new关键字,实例化的时候要声明维度和每个维度的长度。
1:定义数组
数组创建必须有数组大小或数组初始值设定项
声明数组的同时给数组赋值
数据类型+数组标示 = {成员1,成员2,……};
例:
//定义一个数组
int[] array = { 1, 2, 3, 4, 5 };
Console.WriteLine(array[1]); //输出数组中的key值为1的成员 2
创建不始化一个数组
int[] add = new int[3];
add[0] = 9;
add[1] = 10;
Console.WriteLine(add[1]); //10
创建并初始化一个数组(标准的声明方法)
int[] arr = new int[] { 11,22,33}; // 定义数组的另一种方式
Console.WriteLine(arr[0]);
//也可以指定数组长度
int[] a = new int[5] { 000, 1111, 222, 333,555}; // 222
// 长度超出会报错,长度不足也会报错
Console.WriteLine(a[2]);
2:使用for循环遍历数组(与PHP没啥差别)
//定义一个数组
int[] array = { 123, 98, 142, 125, 85 };
int[] a = new int[5] { 000, 111, 222, 333,555}; // 222
int total = 0;
int j = array.Count();
for (int i = 0; i < j; i++)
{
total += array[i];
}
Console.WriteLine(total); // 输出573
int sum = 0;
int bb = a.GetLength(0);//获得数组长度
for (int s = 0; s < bb; s++)
{
sum += a[s];
}
Console.WriteLine(sum); // 输出1221
3:获取数组长度
在实际使用数组的时候,我们经常会遇到一个问题,就是“数组索引超出限制”的报错。
这个解决办法不难:找到你数据的源头,就是想往数组中放多少个元素,在实例化数组的时候,声明对应长度的数组。
Count();
int[] array = { 123, 98, 142, 125, 85 };
int j = array.Count();
GetLength(0)
int[] a = new int[5] { 000, 111, 222, 333,555}; // 222
int bb = a.GetLength(0);//获得数组长度
Length
int[] array = { 123, 98, 142, 125, 85 };
int d = array.Length;
Console.WriteLine(d);
4:使用foreach遍历数组
这里的foreach与PHP不一样,他是没有key值得。
int[] n = new int[10]; /* n 是一个带有 10 个整数的数组 */
/* 初始化数组 n 中的元素 */
for (int i = 0; i < 3; i++)
{
n[i] = i + 100;
Console.WriteLine("Element[{0}] = {1}", i,n[i]);
}
/* 输出每个数组元素的值 */
foreach (int f in n)
{
Console.WriteLine(f);
int i = f - 100; // 变量i是key值
Console.WriteLine("Element[{0}] = {1}", i, f);
}
总结:
for循环遍历数组的时候是有KEY值的。但是其需要知道数组的长度
foreach循环遍历数组的时候是没有KEY值的。不需要知道数组长度。
测试源代码:我这里使用的是控制台应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gc
{
class Program
{
/* C#主要的运行函数,就是main函数 */
static void Main(string[] args)
{
//定义一个数组
int[] array = { 123, 98, 142, 125, 85 };
int d = array.Length;
//Console.WriteLine(d);
//Console.WriteLine(array[1]); //输出数组中的key值为1的成员 98
int[] add = new int[3];
add[0] = 9;
add[1] = 10;
Console.WriteLine(add[1]); //10
int[] arr = new int[3] { 11,22,33}; // 定义数组的另一种方式
//Console.WriteLine(arr[0]);
//也可以指定数组长度
int[] a = new int[5] { 000, 111, 222, 333,555}; // 222
// 长度超出会报错,长度不足也会报错
//Console.WriteLine(a[2]);
int total = 0;
int j = array.Count();
for (int i = 0; i < j; i++)
{
total += array[i];
}
//Console.WriteLine(total); // 输出573
int sum = 0;
int bb = a.GetLength(0);//获得数组长度
for (int s = 0; s < bb; s++)
{
sum += a[s];
}
//Console.WriteLine(sum); // 输出1221
int[] n = new int[10]; /* n 是一个带有 10 个整数的数组 */
/* 初始化数组 n 中的元素 */
for (int i = 0; i < 10; i++)
{
n[i] = i + 100;
//Console.WriteLine("Element[{0}] = {1}", i,n[i]);
}
/* 输出每个数组元素的值 */
foreach (int f in n)
{
//Console.WriteLine(f);
int i = f - 100;
//Console.WriteLine("Element[{0}] = {1}", i, f);
}
Console.ReadKey();
}
}
}
有好的建议,请在下方输入你的评论。
欢迎访问个人博客
https://guanchao.site
欢迎访问小程序: