目录
相对路径(PathData1,后续代码皆选择此文本文件相对路径)
随机生成
int a = rnd.Next();
类型的转换
1、字符串转换成数字:
int a = int.Parse(strArray)
double a = double.Parse(strArray)
2、数字转换成字符串:
10.ToString()
//或者
"" + 10
3、使用 Convert
double a = Convert.ToInt32(strArray);
//或者
double a = Convert.ToDouble("123.45");
//或者
double a = Convert.ToDateTime("2019-09-10 22:00");
文本TXT操作
1、获取文本
相对路径(PathData1,后续代码皆选择此文本文件相对路径)
//读取的为程序bin\Debug下的文件data1.txt
string data = System.AppDomain.CurrentDomain.BaseDirectory;//获取执行程序的相对路径
string PathData1 = data + "data1.txt";
绝对路径
//绝对路径为文件的真实路径,如F:\cs\bin\Debug\data1.txt
2、读取文本行数
//读取文本行数(相对路径PathData1)
using (FileStream fs = new FileStream(PathData1, FileMode.Open, FileAccess.ReadWrite))
{
using (StreamReader reader = new StreamReader(fs))//StreamReader,将文本中的的内容逐行读入
{
while (!reader.EndOfStream)
{
string str = reader.ReadLine();
m1++;//文本行数
}
reader.Close();
}
}
3、读取文本字符串并赋予实数数组
//读入文本字符串数据并赋予到数组(相对路径PathData1)
double[,] Arrary1 = new double[m1, n];
StreamReader rd1 = File.OpenText(PathData1);
for (int i = 0; i < m1; i++)
{
string line = rd1.ReadLine();//从StreamReader流中读取一行字符,并将数据作为字符串返回
line = line.Remove(line.IndexOf('['), 1);//去掉数据里的‘[’和‘]’
line = line.Remove(line.IndexOf(']'), 1);
string[] data = line.Split(' ');//按空格进行元素分割
for (int j = 0; j < n; j++)
{
Arrary1[i, j] = double.Parse(data[j]);//将文本中读取的数据转换为双精度实数,并赋给数组
}
}
4、清空文本内容
//清空文本内容(相对路径PathData1)
FileStream fs = new FileStream(PathData1, FileMode.Create, FileAccess.ReadWrite);
5、将字符串输出到文本
File.WriteAllText("C:/hahaha.txt", a, Encoding.Default);//将字符串a存入文本
6、向文本中添加内容
string path = "C:/hahaha.txt";//文件存放路径,保证文件存在。
StreamWriter txt = new StreamWriter(path, true);
txt.WriteLine("瓜皮");//在文本中添加内容
txt.Close();
字符串操作
1、判断字符串中某个字符的个数
//使用命名空间
using System.Text.RegularExpressions;
//定义字符串
string Email = "@123@456@789@";
//获取@字符出现的次数
int num = Regex.Matches(Email, "@").Count;
Console.WriteLine(num);
2、多个连续空格转为一个空格
str = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(str, " ");
//将多个空格替换为一个空格,避免字符串格式错误。""里为替换内容,也可以换成","等其它符号
3、判断字符串中是否包含某个元素
string a = "COLDPLAY";
if (a.Contains("COLD"))//判断是否包含“COLD”
{
Console.WriteLine("1");
}
一维数组排序
1、从小到大
Array.Sort(arr1);
2、从大到小
Array.Sort(arr1);
Array.Reverse(arr1);//转置
多个一维数组排序
1、准备基础:冒泡排序
static void BubbleSort(double[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])// 在这里判断 第j个元素 和第j+1个元素的大小 如果前者小于后者 则交换值(更改大小排列顺序只需修改大小号)
{
double Temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = Temp;
}
}
}
}
//直接调用方法即可
2、多个一维数组按照某一个一维数组的大小排序方式进行排序
在冒泡排序的基础上,针对多个一维数组按照某一个一维数组的大小,对全部数组进行整体排序。(网上没有找到好方法,自己编制了下面的排序算法,方法比较笨拙)
public static double[] xCk = new double[] { 9, 4, 3, 5, 2 };/
public static double[] yCk = new double[] { 32.4, 15, 73, 431, 523.3 };
public static double[] lk = new double[] { 132, 61.1, 135.5, 27.23, 634 };
static void Main(string[] args)
{
//调用排序方法
MySelectionSort(xCk, yCk, lk);
}
/*arrRefer是一个参考排序数组,arrTarget是一个目标排序数组,根据arrRefer的大小排列顺序对arrTarget数组进行排序;
执行完后,arrRefer和arrTarget都会被重新排序*/
static void MySelectionSort(double[] arrRefer,double[] arrTarget1, double[] arrTarget2)//根据实际情况添加数组
{
for (int i = 0; i < arrRefer.Length; i++)
{
for (int j = 0; j < arrRefer.Length - 1 - i; j++)
{
if (arrRefer[j] > arrRefer[j + 1])//(更改大小排列顺序只需修改大小号)在这里判断第j个元素和第j+1个元素的大小,如果前者小于后者,则交换值
{
//参考排序数组的排列规则,实现for循环迭代
double Temp = arrRefer[j];
arrRefer[j] = arrRefer[j + 1];
arrRefer[j + 1] = Temp;
//目标排序数组依排序数组进行排序
double Target1 = arrTarget1[j];
arrTarget1[j] = arrTarget1[j + 1];
arrTarget1[j + 1] = Target1;
//目标排序数组依排序数组进行排序
double Target2 = arrTarget2[j];
arrTarget2[j] = arrTarget2[j + 1];
arrTarget2[j + 1] = Target2;
}
}
}
}
多维数组排序
1、多维数组按某一行(列)排序
由此,可以推断出,如果将多维数组拆分为多个一维数组,即可实现按某一行(列)的大小,进行列(行)变换
方法可以继续调用上部方法
但是要对多维数组进行初始处理:
for (int i = 0; i < Arrary1.GetLength(0); i++)//0代表行数,可替换为1代表列数
{
xCk[i] = Arrary1[i, 0];
yCk[i] = Arrary1[i, 1];
lk[i] = Arrary1[i, 2];
}
//将多维数组按行拆分为多个一维数组
//按行赋值稍微修改代码即可
2、多维数组全部元素排序
将多维数组的的元素全部赋值给一维数组,然后对一维数组进行排序,之后根据等差数列基本思想对二维数组进行重新赋值
int m = array.GetLength(0);//行
int n = array.GetLength(1);//列
int mn = m * n;//元素总数mn
//定义一个一维数组,长度mn
double[] oneArray = new double[mn];
//将多维数组赋值到一维数组
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
// 把二维数组array的每一个元素都赋值给一维数组oneArray,索引使用等差数列思想
oneArray[j + i * n] = array[i, j];
}
}
//排序
Array.Sort(oneArray);
//将一维数组转换为list
List<double> OneList = oneArray.ToList();
//分割
List<double> sub = new List<double>();
for (int i = 0; i < m; i++)
{
int j = m * i;
sub.Add(OneList[j]);
sub.Add(OneList[j + 1]);
sub.Add(OneList[j + 2]);
sub.Add(OneList[j + 3]);
sub.Add(OneList[j + 4]);
}
//OneList即为排序后结果
//可以直接用OneList继续进行操作
//或者将其转换为数组,效果一样
复制数组元素
int[] a = {1,2,3,4,5};
int[] b = {2,3,4,5,6,7,8,9};
Array.Copy(a,0,b,0,a.Length);//从a的第0个元素开始,length个元素,到目标b数组的第0个元素开始复制
互换数组对应元素
Button[,] buttons = new Button[n,n];//二维数组
int a,b,c,d;
swap(buttons[a,b],buttons[c,d]);//调用对应元素,并换位置
删除数组中对应下标(or指定数)的元素
1、移除数组中的某个数
法一:list
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[] { 1, 2, 3, 4, 5 };
List<int> list = a.ToList();//将数组输入list
list.Remove(5);//移除5
Console.Write("删除之后的数组是:");
showlist(list);
}
static void showlist(List<int> list)//遍历list的函数
{
foreach (var item in list)
{
Console.Write(item);
}
Console.WriteLine();
}
}
}
法二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] arrOld = new int[] { 1, 2, 3, 4, 5, 6, 7 };
int[] arrNew = arrOld.Where(x => x != 3).ToArray();//指向x不等于3的地址,并赋予新数组
for (int i = 0; i < arrNew.Length ; i++)//输出新数组元素
{
Console.WriteLine(arrNew[i]);
}
}
}
}
2、删除数组中对应下标的元素
法一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace other
{
class Program
{
//通过下标删除数组方法DeleteArr
private static int[] DeleteArr(int num, int[] arr)
{
List<int> list = new List<int>();
foreach (int i in arr)
{
list.Add(i);//把数组的每一个元素保存到一个集合中
}
list.Remove(num);//根据集合删除指定下标的元素
arr = new int[list.Count];//重新new一个数组
for (int j = 0; j < list.Count; j++)
{
arr[j] = list[j];//把删除后的集合每一个保存到该数组
}
return arr;//返回该数组
}
static void Main(string[] args)
{
int num = 2;//要删除元素的下标,本例中为下标2
int[] arr = new int[] { 1, 2, 3, 4, 5};
Console.WriteLine("删除前该数组的长度:{0}", arr.Length);
foreach (int a in arr)
{
Console.Write(a + "\t");
}
arr = DeleteArr(num, arr);//调用通过下标删除数组方法DeleteArr
Console.WriteLine("\n删除后该数组的长度:{0}", arr.Length);
foreach (int a in arr)
{
Console.Write(a + "\t");
}
}
}
}
法二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace other
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, };
//Console.WriteLine(arr.Length + "\n");
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int[] temp = new int[arr.Length];//建一个临时数组temp,用来保存当前数组arr的元素个数
for (int t = 0; t < arr.Length; t++)//将arr的值赋给temp
{
temp[t] = arr[t];
}
arr = new int[temp.Length - 1];//重置arr,将数组长度-1
//Console.WriteLine(arr.Length + "\n");
int k = 0; //将temp再赋给arr。
for (int t = 0; t < temp.Length; t++)
{
if (t != 2)//第三个删除
{
arr[k] = temp[t];
k++;
//Console.WriteLine(arr.Length);
Console.WriteLine(arr[k - 1]);
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
}
}
List列表操作
public List<string> ListAll;
void Start()
{
ListAll = new List<string>() { "我", "不", "去", "你", "那" };
//遍历list
foreach (var item in ListAll)
{
Debug.Log(item);
}
}
void RemoveList()//移除
{
//删除List中的元素用for,用foreach会报错
for (int i = 0; i < ListAll.Count; i++)
{
if (ListAll[i] == "我")
{
ListAll.Remove(ListAll[i]);
}
}
foreach (var item in ListAll)
{
Debug.Log("移除后的:" + item);
}
}
void AddList()//添加
{
ListAll.Add("这是什么玩意");
foreach (var item in ListAll)
{
Debug.Log("添加后:" + item);
}
}
计时器
using System.Diagnostics;
Stopwatch watch = new Stopwatch();
watch.Start();
//中间放执行的程序
watch.Stop();
Debug.Log("时间为:" + watch.ElapsedMilliseconds + "毫秒");