c#随笔记02
数组 批量处理数据
数组时一种相同类型的、用一个标识符封装到一起的基本类型数据,可以使用一个统一的数组名和索引来唯一确定数组中的每个元素。
- 数组的索引从0开始。
int[] a=new int[80];
数据类型 标识符 正整数常量
一维数组
一组相同类型数据的线性集合。
创建
数组作为对象允许使用new关键字进行内存分配。在使用数组之前,必须首先定义数组变量所属的类型。
- 先声明,再用new关键字进行内存分配
数组元素类型[] 数组名字;
声明后还不能访问任何元素。
要想真正使用数组,需要为它分配内存空间。在为数组分配内存空间时,必须指明数组长度。
数组名字=new 数组元素类型[数组元素的个数];
- 声明的同时为数组分配内存
数组元素类型[] 数组名=new 数组元素类型[数组元素个数];
初始化
为单个数组赋值
arr[0]=1;
同时为整个数组赋值
string[] arrStr=new string[2]{"sun","mon"};
string[] arrStr=new string[]{"sun","mon"};
string[] arrStr={"sun","mon"};
二维数组
type[,] arrayName;
type[][] arrayName;
直接为每一维分配空间
int[,] a=new int[2,4];//定义一个2行4列的int类型二维数组
分别为每一维分配空间
int[][] a=new int[2][];
a[0]=new int[2];
a[1]=new int[3];
为单个二维元素赋值
int[,] myarr=new int[2,2];
myarr[0,0]=0;
myarr[0,1]=1;
myarr[1,0]=1;
myarr[1,1]=2;
嵌套循环赋值
int[,] myarr=new int[2,2];
for (int i=0;i<2;i++){
for(int j=0;j<2;j++){
myarr[i,j]=i+j;
}
}
为每一维元素赋值
int[][] myarr=new int[2][];
myarr[0]=new int[]{0,1};
myarr[1]=new int[]{1,2};
同时为整个二维数组赋值
int[,] arrStr=new int[2,2]{{1,2},{2,3}};
int[,] arrStr=new int[,]{{1,2},{2,3}};
int[,] arrStr={{1,2},{2,3}};
模拟客车售票系统
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace studyDaily
{
class Program
{
static void Main(string[] args)
{
//设置标题
Console.Title = "售票系统";
//定义座位数组
string[,] seat = new string[9, 4];
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 4; j++)
{
//初始化座位数组
seat[i, j] = "【有票】";
}
}
//定义字符串变量
string s = string.Empty;
while (true)
{
//清空控制台信息
Console.Clear();
//输出字符串
Console.WriteLine("\n 客车售票系统" + "\n");
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 4; j++)
{
//输出售票信息
System.Console.Write(seat[i, j]);
}
//换行
Console.WriteLine();
}
Console.Write("请输入座位行号和列号,(如0,1)输入q键退出:");
//输入售票信息
s = Console.ReadLine();
if (s == "q") break;
//拆分输入的字符串
string[] ss = s.Split(',');
//得到输入的行
int row = int.Parse(ss[0]);
//得到输入的列
int column = int.Parse(ss[1]);
//标记为售出
seat[row, column] = "【售出】";
}
}
}
}
不规则数组
int[][] a=new int[3][];//创建二维数组,指定行数,不指定列数
a[0]=new int[5];
a[1]=new int[4];
a[2]=new int[2];
数组与Array类
C#中的数组是由System.Array类派生而来的引用对象
可以使用Array类中的各种属性或者方法来对数组进行各种操作
- Array类的Length属性获取数组元素的长度
- Array类的Rank属性获取数组的维度
- Copy:将数组中的指定元素复制到另一个Array中
- CopyTo:从指定的目标索引处开始,将当前一维数组中的所有元素复制到另一个一维数组中
- Exists:判断数组中是否包含指定元素
- GetLength:获取Array的指定维中的元素数
- GetLowerBound:获取Array中指定维度的下限
- GetUpperBound:获取Array中指定维度的上限
- GetValue:获取Array中指定位置的值
- Reverse:反转一维Array中元素的顺序
- Setvalue:设置Array中指定位置的元素
- Sort:对一维Array数组元素进行排序
打印杨辉三角
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace studyDaily
{
class Program
{
static void Main(string[] args)
{
//定义一个10行的二维数组
int[][] Array_int = new int[10][];
//向数组中记录杨辉三角的值
//遍历行
for(int i = 0; i < Array_int.Length; i++)
{
//定义列
Array_int[i] = new int[i + 1];
//遍历列
for(int j = 0; j < Array_int[i].Length; j++)
{
//如果是数组的前两行
if (i <= 1)
{
Array_int[i][j] = 1;//值设为1
continue;
}
else
{
if (j == 0 || j == Array_int[i].Length - 1)//如果是行首或行尾
Array_int[i][j] = 1;//将其设为1
else//根据杨辉算法进行计算
Array_int[i][j] = Array_int[i - 1][j - 1] + Array_int[i - 1][j];
}
}
}
for(int i = 0; i <= Array_int.Length - 1; i++)//输出杨辉三角
{
//循环控制每行前面打印的空格数
for (int k = 0; k <= Array_int.Length - i; k++)
{
Console.Write(" ");
}
//循环控制每行打印的数据
for(int j = 0; j < Array_int[i].Length; j++)
{
Console.Write("{0} ", Array_int[i][j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
数组的基本操作
使用foreach语句遍历数组
foreach(【类型】 【迭代变量名】 in 【集合】){
语句
}
对数组进行排序
Sort
Array.Sort方法用于对一维Array中的元素进行排序
public static void Sort(Array array)
public static void Sort(Array array,int index,int length)
- index:起始索引
- length:排序范围内的元素数
int[] arr=new int[]{1,3,65,4,23,,4,5};
Array.Sort(arr);
数组不能为空,只能是一维数组
Reverse
反转一维Array中元素的顺序
public static void Reverse(Array array)
public static void Reverse(Array array,int index,int length)
int[] arr=new int[]{1,3,65,4,23,,4,5};
Array.Reverse(arr);
获取二维数组的列数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace studyDaily
{
class Program
{
static void Main(string[] args)
{
int[][] arr = new int[3][];
arr[0] = new int[5];
arr[1] = new int[3];
arr[2] = new int[4];
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
Console.WriteLine(arr[i][j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}