一:一维数组
1.数组是一种包含若干个变量的数据结构,这些变量具有相同的数据类型,并且排列有序。
有一维数组,多维数组,和不规则数组。
2. 数组的定义 数组是一个引用类型,实际的数组通过new运算符在运行时动态产生。在声明的时候不需要给出具体的元素个数。
3. 数组【】的基本类型:13中数据类型(int、 string 等等)或者自定义【class】类型
4. 数组【】的初始化 规则:(为元素赋值的过程)
1.明确了数组【】中每一个元素的值。
2.明确数组【】的长度。
3.明确数组类型,自定义数组名称变量
注: 数组的长度一旦确定,长度是不能再改变的。
数组是弥补变量只能存放一个数据的局限性,集合是弥补数组长度不可更改的特性。
静态初始化 直接使用{ } 进行初始化
int[] array1 = {10,20,30};
//---------以下是分成两行代码编写-------------
int[] array11;
//array11 = {10,20,30};错误的写法
使用规则:明确数组【】中元素的个数,和每一个原素的值。
动态初始化:使用关键字new进行初始化。
int[] array2 = new int[]{10,20,30};
int[] array3 = new int[3]{10,20,30};
int[] array22 ;
array22 = new int[]{10,20,30};
int[] array33 ;
array33 = new int[3]{10,20,30};
//以下写法错误,元素数量必须和{}内的数据对应。
//int[] array44 = new int[4]{};
int[] numbers =new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
使用规则:已知存储数据的个数,但是并不知道每一个原色的具体的值。
数组初始化为默认值0,0.0,\0 , null
5.数组的每一个元素的值 通过数组名.[下标 ]
数组的属性Length:
数组【】array ; int array.Length;// 获取数组的长度,数组中元素的个数
------------------------------------------------数组的理解----------------------------------------------------------------
int【】 number = new int【3】;
1.数组的名称为 ? number
2.数组的长度是 ? 3、number.Length
3.数组的类型? int
4.数组中的有几个元素? 3个,number.Length
5.数组中的第二个元素如何表示? number【1】
6.对于number【1】的下标是? 1
7.数组元素第一个元素的表示方法 ? number【0】;
8.数组元素最后一个元素的表示方法? number【2】、number【number.Length-1】;
-------------------------------------------------------------------------------------------------------------------------------
6.数组的遍历
使用for循环进行 遍历
for(int i = 0 ; i< array.Length-1 ; i++)
{
Console.WriteLine(array[i]);
}
7.数组越界异常
System.IndexOutOfRangeException;索引超出了数组
练习:自定义一个int类型的数组,输入3个数字元素,并检查输入数据的是int类型。
方法一:
using System;
public class Program
{
public static void Main()
{
int[] arr =new int[3];
int index = 0;
while(true)
{
try {
Console.WriteLine("input a number?... {0}",index);
string str = Console.ReadLine();
arr[index] = int.Parse(str);
//当转化异常下面的代码不执行:index++
index++;
}catch
{
Console.WriteLine("输入错误重新输入");
}
if(index==3)
{
break;
}
}
foreach(var i in arr)
{
Console.Write(i+"\t");
}
Console.WriteLine("-----END");
}
}
方法二:
using System;
public class Program
{
public static void Main()
{
MyArray();
}
public static void MyArray()
{
int[] array = new int[3];
for(int i = array.Length-1 ; i>=0;i--)
{
try{
Console.WriteLine("第{0}个元素的值",i);
array[i] = Convert.ToInt32(Console.ReadLine());
}catch
{
//当输入错误的时候,执行i++操作返回上一步的的执行。
Console.WriteLine("输入错误重新输入...");
i++;
}
}
foreach(var i in array )
{
Console.WriteLine(i);
}
}
}
强制类型转换: (输入foreach快速按两下Tab键,能快速生成相应的代码块...)
int a = 10;
float b = (float)a/4; //此时先行将a转换为float类型,在进行预算。
float c = (float)(a/4);
Console.WriteLine("---"+b); //2.5
Console.WriteLine("---"+c); //2
数组工具类:Array
命名空间: nameSpace System;
public abstract class Array { }
public static void Copy(Array sourceArray, Array destinationArray, long length) { }
public static void Copy(Array sourceArray, long sourceIndex, Array destinationArray, long destinationIndex, long length){ }
public static void Sort(Array array){ }
public static void Sort(Array keys, Array? items){ }
public static void Sort(Array array, int index, int length){ }
public static void Reverse(Array array){ }
public static void Reverse(Array array, int index, int length){ }
public static void Reverse<T>(T[] array){ }
public void Clear(){ }
8.数组的拷贝 void Array.Copy( );
9. 数组的排序 void Array.Sort( ); 改变原数组中数组的排序,从小到大排列
10.数组的反转 void Array.Reverse( );改变原数组中数组的排序,反转数组
11.数组的清空 void Array.Clear( ) ; 清空数组元素;
12.数组的插入
using System;
public class Program
{
public static void Main()
{
int[] array = {10,20,30,60,80,50,40,90,70,110};
int[] array1 = new int[11];
Array.Sort(array);//数组的排序,参数(数组名)
Array.Reverse(array);//数组的反转操作,参数(数组名)
Array.Copy(array,array1,6);//数组的复制操作,参数(原数组名,目标数组名,数组长度)
//注:长度如果超过array1.length的长度会报错,不足的长度数据为默认值。
Console.WriteLine("array 排序,反转后");
foreach(int i in array){ Console.Write(i+"\t");}
Console.WriteLine("\r\narray1 复制array ");
foreach(int i in array1){ Console.Write(i+"\t");}
Console.WriteLine("\r\narray1,清空数组,把【0】赋值为10 ");
Array.Clear(array1,3,2);//清空数组操作(不是删除,只是会变成默认值),
//参数(数组名,从哪个开始,长度)(从0开始3个元素)
array1[0]=10;//清空数组,数组内的元素全部变成默认值0,0.0,\0,null;
foreach(int i in array1){ Console.Write(i+"\t");}
Console.WriteLine("\r\n----------End----------");
}
}
---------------------
array 排序,反转后
110 90 80 70 60 50 40 30 20 10
array1 复制array
110 90 80 70 60 50 0 0 0 0 0
array1,清空数组,把【0】赋值为10
10 90 80 0 0 50 0 0 0 0 0
----------End----------
二 :二维数组的基本定义和初始化
1. 二维数静态赋值:(直接赋值)
int[,] sores = {{10,20},{20,36},{69,30}};
2. 二维数动态赋值:(使用new关键字)
int[,] sores0 = new int[3,2];
sores0 = {{10,20},{20,36},{69,30}};
int[,] sores1 = new int[3,2]{{10,20},{20,36},{69,30}};
3.二维数组的标示
int[,] sores0 = new int[3,2];
sores0 = {{10,20},{20,36},{69,30}};
int[,] sores1 = new int[3,2]{{10,20},{20,36},{69,30}};
sores1[0][0] ; // 值是3 第一个元素
总结:
一维数组
type[] arrayName;
//type 是C#的任意数据类型
//[] 表示的是一个数组
//arrayName 是数组名,遵循命名规则
创建数组对象:使用new运算符创建数组实例:有两种形式;
type[] arrayName;
arrayName = new type[size];
//size 表示数组的个数
//以上也可以合并书写
type[] arrayName = new type[size];
多维数组:
type[ , , ] arrayName ;
二维数组的遍历:
score[0,0] score[0,1] score[0,2]
score[1,0] score[1,1] score[1,2]
score[2,0] score[2,1] score[2,2]
int[,] sores1 = new int[,]{{10,20},{20,36},{69,30}};
//二维数组的遍历:
for(int i =0;i<3;i++)
{
for(int j = 0;j<2;j++)
{
Console.WriteLine(sores1[i,j] );
}
}
using System;
public class Program
{
public static void Main()
{
int[,] sores0 = new int[3,2];
//sores0 = {{10,20},{20,36},{69,30}};
int[,] sores1 = new int[,]{{10,20},{20,36},{69,30}};
int[,] sores2 = new int[3,2]{{10,20},{20,36},{69,30}};
//sores1.Length;
for(int i =0;i<sores1.GetLength(0);i++)
{
for(int j = 0;j<sores1.GetLength(1);j++)
{
Console.Write(sores1[i,j]+" " );
}
Console.WriteLine(sores1.Length);
}
//-----------------------------
int[,] array = new int[,] {{10,20,30},{40,50,60},{70,80,90}};//定义一个3行3列的二维数组
int row = array.Rank; //获取维数,行数
int col = array.GetLength(1);//获取指定维度中的元素个数,这里也就是列数了。(0是第一维,1表示的是第二维)
int col1 = array.GetUpperBound(0)+1;//获取指定维度的索引上限,在加上一个1就是总数,这里表示二维数组的行数
int num = array.Length;//获取整个二维数组的长度,即所有元的个数
Console.WriteLine(row);//2
Console.WriteLine(col);//3
Console.WriteLine(col1);//3
Console.WriteLine(num);//9
//根据上述我们可以自己验证多维数组的形式,使用循环遍历操作数组,例如下面的四维数组:
int[,,,,] arr = new int[10, 20, 30, 40,50];
Console.WriteLine(arr.Rank);//返回4
Console.WriteLine( arr.GetLength(0));//返回10
Console.WriteLine( arr.GetLength(1));//返回20
Console.WriteLine( arr.GetLength(2));//返回30
Console.WriteLine( arr.GetLength(3));//返回40
Console.WriteLine( arr.GetUpperBound(0)+1);//返回9
Console.WriteLine( arr.Length);//返回12000000
Console.WriteLine( arr.GetLength(0));//10
}
}
一维数组的GetLength(方法的使用)(指定维中的元素的数)
int[] sores111 = new int[3]{10,30,60};
Console.Write(sores111.GetLength(0)); //3
//Console.Write(sores111.GetLength(1)); //System.IndexOutOfRangeException
1288

被折叠的 条评论
为什么被折叠?



