请教c# 对于[1,2,3]这种数据应该如何分开读取?比如 arr[0]=1 arr[1]=2

C#数组。1:多维数组定义二位数组int[,] array = new int[,] {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
使用for foreach循环遍历二维数组
// 使用for循环遍历二位数组
for (int i = 0; i < array.GetLength(0); i++)
{
for (int f = 0; f < array.GetLength(1); f++)
{
//Console.WriteLine(array[i,f]);
}
}
// 使用foreach遍历二位数组(遍历多维数组用foreach简单的多)
foreach (var item in array)
{
//Console.WriteLine(item);
}
定义三维数组// 定义三维数组
int[,,] arr = new int[,,] {
{
{1, 2}
},
{
{4, 5}
}
};
使用for foreach循环遍历三维数组foreach (var val in arr)
{
//Console.WriteLine(val);
//输出 1245
}
// for循环
for (int s = 0; s < arr.GetLength(0); s++)
{
for (int a = 0; a < arr.GetLength(1); a++)
{
for (int g = 0; g < arr.GetLength(2); g++)
{
//Console.WriteLine(arr[s,a,g]);
//输出 1245
}
}
}
2:交错数组(可变数组)声明一个交错数组 a,a 中有三个元素,分别是 a[0],a[1],a[2] 每个元素都是一个数组在声明交错数组的时候,只需要指定一维数组的长度。int[][] ab = new int[3][];
//以下是声明交错数组的每一个元素的,每个数组的长度可以不同
ab[0] = new int[] { 1, 2, 3 };
ab[1] = new int[] { 4, 5, 6, 7, 8 };
ab[2] = new int[] { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
交错数组的遍历/* 一个由 5 个整型数组组成的交错数组 */
int[][] aaa = new int[][] { new int[] { 0, 0 }, new int[] { 1, 2 }, new int[] { 2, 4 }, new int[] { 3, 6 }, new int[] { 4, 8 } };
/* 输出数组中每个元素的值 */
for (int ii = 0; ii < 5; ii++)
{
for (int jj = 0; jj < 2; jj++)
{
//Console.WriteLine("aaa[{0}][{1}] = {2}", ii, jj, aaa[ii][jj]);
}
}
// foreach 输出
foreach(var value in aaa){
//Console.WriteLine(value);
foreach (var item in value)
{
//Console.WriteLine(item);
}
}
3:C# Array类下边是数组的属性// 定义一个二维数组
int[,] qq = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
IsFixedSize 获取一个值,该值指示数组是否带有固定大小。
bool res = qq.IsFixedSize;
Console.WriteLine(res); //输出true
IsReadOnly 获取一个值,该值指示数组是否只读。
bool ult = qq.IsReadOnly;
Console.WriteLine(ult);
//输出false
Length 获取一个 32 位整数,该值表示所有维度的数组中的元素总数。int total = qq.Length;
Console.WriteLine(total);
//输出12
LongLength 获取一个 64 位整数,该值表示所有维度的数组中的元素总数。也就是说length不够用的时候 用LongLengthint totals = qq.Length;
Console.WriteLine(total);
//输出12
Rank 获取数组的秩(维度)。int asd = qq.Rank;
Console.WriteLine(asd);
//2
此为二维数组
下边是数组的方法Clear 根据元素的类型,设置数组中某个范围的元素为零、为 false 或者为 null。Array.Clear(qq,0,qq.Length);
//清空数组(前边的Array是固定的)
foreach (var item in qq)
{
Console.WriteLine(item);
//输出 0/0/0/0/0/0/0/0/0/0/0/0
}
Copy(Array, Array, Int32)从数组的第一个元素开始复制某个范围的元素到另一个数组的第一个元素位置。长度由一个 32 位整数指定。Array.Copy(qq, array, 2);
foreach (var item in array)
{
Console.WriteLine(item);
// 输出0、0、3、4、5、6、7、8、9、10、11、12
}
CopyTo(Array, Int32)从当前的一维数组中复制所有的元素到一个指定的一维数组的指定索引位置。索引由一个 32 位整数指定。int[] one = new int[] {1,2,3,4,5,6,7};
int[] two = new int[] { 11, 22, 33, 44, 55, 66, 77 ,88,99,100};
one.CopyTo(two,1);
foreach (var item in two)
{
Console.WriteLine(item); //输出 11,1,2,3,4,5,6,7,99,100
}
GetLength 获取一个 32 位整数,该值表示指定维度的数组中的元素总数。
int nums = one.GetLength(0);
Console.WriteLine(nums);
//输出7
GetLongLength 获取一个 64 位整数,该值表示指定维度的数组中的元素总数。也就是说GetLongLength 不够用了,使用GetLongLengthint numss = two.GetLength(0);
Console.WriteLine(numss);
//输出10
GetLowerBound 获取数组中指定维度的下界。int rand = array.GetLowerBound(1);
Console.WriteLine(rand);
//输出0
GetUpperBound 获取数组中指定维度的上界。
int randss = array.GetUpperBound(0);
Console.WriteLine(randss);
//输出3
GetType 获取当前实例的类型。从对象(Object)继承Console.WriteLine(one.GetType());
//System.Int32[]
GetValue(Int32)获取一维数组中指定位置的值。索引由一个 32 位整数指定。
int[] three = new int[] { 1, 2, 3, 4, 5, 6, 7 };
int we = (int)three.GetValue(3);
// GetValue获取的是一个对象。
Console.WriteLine(we);
IndexOf(Array, Object) 搜索指定的对象,返回整个一维数组中第一次出现的索引。int vv = Array.IndexOf(three,3);
Console.WriteLine(vv);
// 输出2
Reverse(Array)逆转整个一维数组中元素的顺序。Array.Reverse(three);
foreach (var item in three)
{
Console.WriteLine(item); // 输出7/6/5/4/3/2/1
}
Sort(Array)使用数组的每个元素的 IComparable 实现来排序整个一维数组中的元素。Array.Sort(three);
foreach (var item in three)
{
Console.WriteLine(item);
// 输出1/2/3/4/5/6/7
}
SetValue(Object, Int32) 给一维数组中指定位置的元素设置值。索引由一个 32 位整数指定。three.SetValue(22, 1);
foreach (var item in three)
{
Console.WriteLine(item); // 输出1/22/3/4/5/6/7
}
ToString 返回一个表示当前对象的字符串。从对象(Object)继承。string str = three.ToString();
Console.WriteLine(str);
//输出System.Int32[]
测试使用代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gc
{
class Program
{
/* C#主要的运行函数,就是main函数 */
static void Main(string[] args)
{
//定义二位数组
int[,] array = new int[,] {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
// 使用for循环遍历二位数组
for (int i = 0; i < array.GetLength(0); i++)
{
for (int f = 0; f < array.GetLength(1); f++)
{
//Console.WriteLine(array[i,f]);
}
}
// 使用foreach遍历二位数组(遍历多维数组用foreach简单的多)
foreach (var item in array)
{
//Console.WriteLine(item);
}
// 定义三维数组
int[,,] arr = new int[,,] {
{
{1, 2}
},
{
{4, 5}
}
};
foreach (var val in arr)
{
//Console.WriteLine(val);
//输出 1245
}
// for循环
for (int s = 0; s < arr.GetLength(0); s++)
{
for (int a = 0; a < arr.GetLength(1); a++)
{
for (int g = 0; g < arr.GetLength(2); g++)
{
//Console.WriteLine(arr[s,a,g]);
//输出 1245
}
}
}
// 交错数组(可变数组)
// 声明一个交错数组 a,a 中有三个元素,分别是 a[0],a[1],a[2] 每个元素都是一个数组
int[][] ab = new int[3][];
//以下是声明交错数组的每一个元素的,每个数组的长度可以不同
ab[0] = new int[] { 1, 2, 3 };
ab[1] = new int[] { 4, 5, 6, 7, 8 };
ab[2] = new int[] { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
/* 一个由 5 个整型数组组成的交错数组 */
int[][] aaa = new int[][] { new int[] { 0, 0 }, new int[] { 1, 2 }, new int[] { 2, 4 }, new int[] { 3, 6 }, new int[] { 4, 8 } };
/* 输出数组中每个元素的值 */
for (int ii = 0; ii < 5; ii++)
{
for (int jj = 0; jj < 2; jj++)
{
//Console.WriteLine("aaa[{0}][{1}] = {2}", ii, jj, aaa[ii][jj]);
}
}
// foreach 输出
foreach(var value in aaa){
//Console.WriteLine(value);
foreach (var item in value)
{
//Console.WriteLine(item);
}
}
// C# Array 类
int[,] qq = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
//IsFixedSize
获取一个值,该值指示数组是否带有固定大小。
bool res = qq.IsFixedSize;
//Console.WriteLine(res); //输出true
//
IsReadOnly
获取一个值,该值指示数组是否只读。
bool ult = qq.IsReadOnly;
//Console.WriteLine(ult);
//输出false
// Length
获取一个 32 位整数,该值表示所有维度的数组中的元素总数。
int total = qq.Length;
//Console.WriteLine(total);
//输出12
// LongLength 获取一个 64 位整数,该值表示所有维度的数组中的元素总数。
// 也就是说length不够用的时候
用LongLength
int totals = qq.Length;
//Console.WriteLine(total);
//输出12
//Rank
获取数组的秩(维度)。
int asd = qq.Rank;
//Console.WriteLine(asd);
//2
此为二维数组
//
Clear 根据元素的类型,设置数组中某个范围的元素为零、为 false 或者为 null。
Array.Clear(qq,0,qq.Length);
//清空数组(前边的Array是固定的)
foreach (var item in qq)
{
//Console.WriteLine(item);
//输出 0/0/0/0/0/0/0/0/0/0/0/0
}
// Copy(Array, Array, Int32)从数组的第一个元素开始复制某个范围的元素到另一个数组的第一个元素位置。长度由一个 32 位整数指定。
Array.Copy(qq, array, 2);
foreach (var item in array)
{
//Console.WriteLine(item);
// 输出0、0、3、4、5、6、7、8、9、10、11、12
}
int[] one = new int[] {1,2,3,4,5,6,7};
int[] two = new int[] { 11, 22, 33, 44, 55, 66, 77 ,88,99,100};
// CopyTo(Array, Int32)
// 从当前的一维数组中复制所有的元素到一个指定的一维数组的指定索引位置。索引由一个 32 位整数指定。
one.CopyTo(two,1);
foreach (var item in two)
{
//Console.WriteLine(item); //输出 11,1,2,3,4,5,6,7,99,100
}
// GetLength 获取一个 32 位整数,该值表示指定维度的数组中的元素总数。
int nums = one.GetLength(0);
//Console.WriteLine(nums);
//输出7
//GetLongLength
获取一个 64 位整数,该值表示指定维度的数组中的元素总数。
// 也就是说GetLongLength 不够用了,使用GetLongLength
int numss = two.GetLength(0);
//Console.WriteLine(numss);
//输出10
// GetLowerBound 获取数组中指定维度的下界。
int rand = array.GetLowerBound(1);
//Console.WriteLine(rand);
//输出0
//GetUpperBound 获取数组中指定维度的上界。
int randss = array.GetUpperBound(0);
//Console.WriteLine(randss);
//输出3
// GetType 获取当前实例的类型。从对象(Object)继承
//Console.WriteLine(one.GetType());
//System.Int32[]
int[] three = new int[] { 1, 2, 3, 4, 5, 6, 7 };
// GetValue(Int32)获取一维数组中指定位置的值。索引由一个 32 位整数指定。
int we = (int)three.GetValue(3);
// GetValue获取的是一个对象。
//Console.WriteLine(we);
//IndexOf(Array, Object) 搜索指定的对象,返回整个一维数组中第一次出现的索引。
int vv = Array.IndexOf(three,3);
//Console.WriteLine(vv);
// 输出2
// Reverse(Array)逆转整个一维数组中元素的顺序。
Array.Reverse(three);
foreach (var item in three)
{
// Console.WriteLine(item); // 输出7/6/5/4/3/2/1
}
//Sort(Array)使用数组的每个元素的 IComparable 实现来排序整个一维数组中的元素。
Array.Sort(three);
foreach (var item in three)
{
Console.WriteLine(item);
// 输出1/2/3/4/5/6/7
}
// SetValue(Object, Int32) 给一维数组中指定位置的元素设置值。索引由一个 32 位整数指定。
three.SetValue(22, 1);
foreach (var item in three)
{
Console.WriteLine(item); // 输出1/22/3/4/5/6/7
}
//ToString 返回一个表示当前对象的字符串。从对象(Object)继承。
string str = three.ToString();
Console.WriteLine(str);
//输出System.Int32[]
}
}
}
数组暂时先看到这里,之后用的时候再补充。有好的建议,请在下方输入你的评论。欢迎访问个人博客https://guanchao.site欢迎访问小程序:

我要回帖

更多关于 可重入函数不可以调用不可重入函数 的文章

 

随机推荐