一、可空类型
nullable:表示其基础值类型正常范围内的值,再加上一个null值。
二、数组
(1)简单数组
array.Length:数组的长度
array.Rank:数组从零开始的维数
array.SetValue(1,2):数组进行替换
1表示替换的内容
2表示索引位置
(2)多维数组
string[,] names;
array.GetLength:不傲视数组的指定维中的元素数
(3)三维数组
int[,]names;
(4)交错数组
是数组中的数组,可以声明一个带有int值的交错数组。
int [][] scores;
static void Main(string[] args)
{
//简单数组
int[] array = { 1, 2, 3 };
Console.WriteLine(array.Length);
Console.WriteLine(array.Rank);
Console.WriteLine();
//二维数组
int[,] array1 = new int[3, 2];
array1[0, 0] = 1;
array1[0, 1] = 2;
array1[1, 0] = 3;
array1[1, 1] = 4;
array1[2, 0] = 5;
array1[2, 1] = 6;
for (int i=0; i<array1.GetLength(0); i++)
{
for (int j=0;j<array1.GetLength(1);j++)
{
Console.Write(array1[i, j] + " ");
}
}
Console.WriteLine();
//三维数组
//int[,,] array2 = new int[3, 2, 1];
//array2[0, 0, 0] = 1;
//array2[0, 1, 0] = 2;
//array2[1, 0, 0] = 4;
//array2[1, 1, 0] = 5;
//array2[2, 0, 0] = 7;
//array2[2, 1, 0] = 8;
int[,,] array3 = new int[3, 2, 2];
array3[0, 0, 0] = 1;
array3[0, 0, 1] = 2;
array3[0, 1, 0] = 3;
array3[0, 1, 1] = 4;
array3[1, 0, 0] = 5;
array3[1, 0, 1] = 6;
array3[1, 1, 0] = 7;
array3[1, 1, 1] = 8;
array3[2, 0, 0] = 9;
array3[2, 0, 1] = 10;
array3[2, 1, 0] = 11;
array3[2, 1, 1] = 12;
for (int i = 0; i < array3.GetLength(0); i++)
{
for (int j = 0; j < array3.GetLength(1); j++)
{
for (int k = 0; k < array3.GetLength(2); k++)
{
Console.Write(array3[i,j,k]+" ");
}
}
}
Console.WriteLine();
//交错数组
//int[][] array4 = new int[2][];
//array4[0] = new int[] { 1, 2, 3 };
//array4[1] = new int[] { 4, 5, 6 };
int[][] array4 = new int[2][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
for (int i = 0; i < array4.Length; i++)
{
for (int j = 0; j < array4[i].Length; j++)
{
Console.Write(array4[i][j]+" ");
}
}
Console.WriteLine();
Console.ReadLine();
三、泛型集合(动态数组)
属性:count
命名空间:using System.Collections.Generic;
格式:List<T> myArray=new List<T>(){};
泛型集合常用方法
Add():给集合的最后面添加一个元素
AddRange():给集合的最后面再添加一组元素
Clear():从集合中移除所有元素
Contains():确定某元素是否在集合中
Insert():在指定位置插一个元素
Remove():从集合中移除指定的元素
RemoveAt():从集合中移除指定的索引元素
Reverse():将整个集合中的元素反转
Sort():按照特定的规则对集合中的元素进行排序
案例
//求出下列数组中的最大数、最小数、总和、平均值
//12,33,21,34,55,6
int[] array5 = { 12, 33, 21, 34, 55, 6 };
//冒泡
for (int i = 0; i < array5.Length - 1; i++)
{
for (int j = 0; j < array5.Length - 1 - i; j++)
{
if (array5[j] > array5[j + 1])
{
int temp = array5[j + 1];
array5[j + 1] = array5[j];
array5[j] = temp;
}
}
}
//{0}:占位符
Console.WriteLine("最大值:{0}",array5[array5.Length-1]);
Console.WriteLine("最小值:" + array5[0]);
double sum = 0;
for (int i = 0; i < array5.Length; i++)
{
sum += array5[i];
}
Console.WriteLine("总和:" + sum);
double avg = sum / array5.Length;
Console.WriteLine("平均值:" + avg);
//第二种方法
Console.WriteLine("Max:{0}", array5.Max());
Console.WriteLine("Min:{0}", array5.Min());
Console.WriteLine("Sum:{0}", array5.Sum());
Console.WriteLine("Average:{0}", array5.Average());
//将下列整数数组的每个元素进行如下处理:如果元素是正数则加1,如果是负数则减1,如果是0,则不变。将最终结果输出
//3,-21,0,44,-2,33,-12
int[] a = { 3, -21, 0, 44, -2, 33, -12 };
//List<int> a = new List<int>() { 3, -21, 0, 44, -2, 33, -12 };
for (int i = 0; i < a.Length; i++)
{
if (a[i]>0)
{
Console.WriteLine(++a[i]);
}
if(a[i]<0)
{
Console.WriteLine(--a[i]);
}
if (a[i]==0)
{
Console.WriteLine(a[i]);
}
}
//将一个字符串数组的元素顺序进行反转。{“我”,“是”,“中国人”}
//最终结果{“中国人”,“是”,“我”}
//集合
List<string> str = new List<string>() { "我", "是", "中国人" };
str.Reverse();
for (int i = 0; i < str.Count; i++)
{
Console.Write(str[i]+" ");
}
Console.WriteLine();
string[] str1 = {" 我", "是", "中国人"};
string[] str2 = new string[str1.Length];
for (int i = 0; i < str1.Length; i++)
{
str2[i] = str1[str1.Length - 1 - i];
}
for (int j = 0; j < str1.Length; j++)
{
Console.Write(str2[j] + " ");
}
Console.ReadLine();
}
}
数组和泛型集合的区别
数组是一个同类型变量的集合,数组长度不可改变,方法单调;
泛型集合又称动态数组,长度可改变,方法不单调。