using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
//普通数组和泛型数组的异同
//1.相同点:它们都可以约束内部成员的数据类型
//2.相同点:都是有序容器
//3.不同点:普通数组有容器上限 泛型没有
//4.不同点:操作不同 泛型数组明显简洁 普通数组相对繁琐
//泛型数组与动态数组的异同
//1.相同点:动态的数组
//2.相同点:有序的容器
//3.相同点:方法相同
//4.不同点:参数不同 泛型数组Add参数是程序员指定好的类型 动态数组Add参数都是object类型
//5.不同点:成员不同 泛型数组成员统一类型 动态数组接收到的实际内容可以有各种类型
//6.不同点:泛型数组 添加无需装箱与拆箱 可以直接使用 而动态数组存值需要装箱和拆箱
namespace 字符串型的泛型数组
{
class Program
{
static void Main(string[] args)
{
//定义字符串的泛型数组
List<String> strList = new List<String>();
//定义长度为10的字符串数组
String[] strArray = new String[10];
//比较两个数组的区别
//添加数据 字符串hello添加 字符串csharp
String a = "hello";
String b = "caharp";
//1.泛型数组添加数据
strList.Add(a);
strList.Add(b);
Console.WriteLine("当前数组的长度为" + strList.Count);
//2.普通数组添加数据
for (int i = 0; i < strArray.Length; i++)
{
//判断当前下标是否为null 如果为null 添加数据
if (strArray[i]==null)
{
strArray[i] = a;
break;
}
}
//添加变量b
for (int i = 0; i < strArray.Length; i++)
{
//判断当前下标是否为null 如果为null 添加数据
if (strArray[i] == null)
{
strArray[i] = b;
break;
}
}
//获得普通数组的真是长度
int n = 0;
for (int i = 0; i < strArray.Length; i++)
{
if (strArray[i]!=null)
{
n++;
}
}
Console.WriteLine("当前的数组长度为" + n);
//打印泛型数组的0号索引对应的值
Console.WriteLine("泛型数组的0号的索引值为" + strList[0]);
//删除泛型数组的0号索引
strList.RemoveAt(0);
//再打印一次
Console.WriteLine("泛型数组的0号的索引值为" + strList[0]);
//卡顿
Console.ReadLine();
}
}
}
2021-01-07
最新推荐文章于 2022-03-05 23:09:19 发布