• 二维数组


    二维数组:
    一维数组----豆角
    二维数组----表格

    定义:
    一维数组:
    数据类型[] 数组变量名 = new 数据类型[数组长度];
    数据类型[] 数组变量名 = new 数据类型[数组长度]{1,2,3....};

    二维数组:
    数据类型[,] 数组变量名 = new 数据类型[行数,列数];
    int[,] a = new int[3,4];

    赋值:
    a[行下标,列下标] = 值 下标都是从0开始的
    取值:
    a[行下标,列下标]

    例题:一个班6个人,从键盘输入每个学号语文,数学,外语成绩(不需输入学号)。输出:学生成绩表(包括每个人的总分),每科的平均分。


    int[,] a = new int[6, 5];

    for (int i = 0; i < 6;i++ )
    { Console.Write("请输入第{0}个学生的语文成绩:",i+1);
    int yw = Convert.ToInt32(Console.ReadLine());
    Console.Write("请输入第{0}个学生的数学成绩:",i+1);
    int sx = Convert.ToInt32(Console.ReadLine());
    Console.Write("请输入第{0}个学生的外语成绩:",i+1);
    int wy = Convert.ToInt32(Console.ReadLine());
    a[i, 0] = i + 1;
    a[i, 1] = yw;
    a[i, 2] = sx;
    a[i, 3] = wy;
    a[i, 4] = yw + sx + wy;

    }
    int yp = 0; int sp = 0; int wp = 0;
    for (int i = 0; i <= 5;i++ )
    {
    for (int j = 1; j <= 5
    - i;j++ )
    {
    if (a[i , 4] > a[j - 1, 4])
    {
    int txh = a[j - 1, 0];
    a[j - 1, 0] = a[j, 0];
    a[j, 0] = txh;
    int tyw = a[j - 1, 1];
    a[j - 1, 1] = a[j, 1];
    int tsx = a[j - 1, 2];
    a[j - 1, 2] = a[j, 2];
    a[j, 2] = tsx;
    int twy = a[j - 1, 3];
    a[j - 1, 3] = a[j, 3];
    a[j, 3] = twy;
    int tzf = a[j - 1, 4];
    a[j - 1, 4] = a[j, 4];
    a[j, 4] = tzf;



    }


    }

    for (i = 0; i < 5; i++)
    {
    yp = yp + a[i, 1];
    sp = sp + a[i, 2];
    wp = wp + a[i, 3];

    }
    double avg1 = 1.0 * yp / 3;
    double avg2 = 1.0 * sp / 3;
    double avg3 = 1.0 * wp / 3;
    Console.WriteLine("平均分 {0} {1} {2}", avg1, avg2, avg3);
    }

    Console.WriteLine("学号 语文 数学 外语 总成绩 名次");

    for (int i = 0; i < 6;i++ )
    { Console.WriteLine("{0} {1} {2} {3} {4} {5}",a[i,0],a[i,1],a[i,2],a[i,3],a[i,4],i+1);}

    锯齿数据,数组的数组。
    定义:
    第一步:定义大数组
    数据类型[][] a = new 数据类型[行数][];
    第二步:定义小数组
    数据类型[] a1 = new 数据类型[列数];
    数据类型[] a2 = new 数据类型[列数];
    ......
    第三步:把小数组放到大数组中
    a[0] = a1;
    a[1] = a2;
    ....

    int[,] a = new int [3][4]; //错
    int[][] a = new int[3,4]; //错
    int[][] a = new int[3][4]; //错
    int[,] c = new int[3,4]; //对

    c.Length???? 12;

    int[][] a = new int[3][];

    int[] a1 = new int[] { 3, 4, 5, 6, 7 };
    int[] a2 = new int[] { 1, 2, 3 };
    int[] a3 = new int[] { 7, 8, 9, 10 };

    a[0] = a1;
    a[1] = a2;
    a[2] = a3;
    上面的二级数组中a.Length???? 3;

    集合:

    一、ArrayList 链表,没有长度限制,可以随时向时添加或删除元素。
    需要在前面加上:using System.Collections;

    定义:
    ArrayList a = new ArrayList();
    操作:
    a.Add(数据):添加
    a.Insert(索引号,数据):插入
    a.RemoveAt(索引号):删除
    a.Count 集合中元素的个数

    取值:
    a[下标]
    取出来的值需要进行强制转换。

    二、List<类型> 链表,,没有长度限制,可以随时向时添加或删除元素。只能放指定类型的数据,取出来也不用强制转换。
    定义
    List<类型> 变量名 = new List<类型>();
    List<int> a = new List<int>();
    操作:
    a.Add(数据):添加
    a.Insert(索引号,数据):插入
    a.RemoveAt(索引号):删除
    a.Count 集合中元素的个数

    a.Sort(); 排序
    a.Reverse();反转

    取值
    a[索引号]

    三、Dictionary<key,value>字典或哈希表
    定义
    Dictionary<int,string> a = new Dictionary<int,string>();

    操作:
    a.Add(键值,数据);
    a.Remove(键值);
    a.Count;

    取值:
    a[键值]


    四、栈,队列 知道就行了
    栈:先进后出,不能随机取其中任意一个值。
    Stack<数据类型> a = new Stack<数据类型>();
    a.Push(值);
    数据类型 变量名 = a.Pop();

    队例:先进先出,不能随机取其中任意一个值。
    Queue<int> a = new Queue<int>();
    a.Enqueue(值);
    数据类型 变量 = a.Dequeue();

  • 相关阅读:
    前端
    前端
    数据库
    代码块
    装饰器
    函数 初识函数
    相识python --------文件操作
    相识python --------str字符串 int整形 bool布尔值 tu元祖 set()集合 dict 字典的数据补充
    【openstack报错】【因更新包而致】IncompatibleObjectVersion: Version 1.9 of Instance is not supported
    开博第一篇
  • 原文地址:https://www.cnblogs.com/chenchen0815/p/5427032.html
Copyright © 2020-2023  润新知