一.二维数组

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

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

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

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

题目:一个班6个人,从键盘输入每个学号语文,数学,外语成绩(不需输入学号)。输出:学生成绩表(包括每个人的总分),每科的平均分。
附加1:试着,把不及格的用红字显示。
附加2:试着按照总分排序,显示名次出来。

代码:

 1  static void Main(string[] args)
 2         {
 3         //输入6个学生的语文,数学,英语成绩,输出总分和名次
 4             int[,] a=new int[6,5];
 5             //输入
 6             for (int i = 0; i <6; i++)
 7             {
 8                 Console.Write("请输入第{0}个学生的语文成绩:",i+1);
 9                 int yw = Convert.ToInt32(Console.ReadLine());
10                 Console.Write("请输入第{0}个学生的数学成绩:", i+1);
11                 int sx = Convert.ToInt32(Console.ReadLine());
12                 Console.Write("请输入第{0}个学生的英语成绩:", i + 1);
13                 int yy = Convert.ToInt32(Console.ReadLine());
14
15                 a[i, 0] = i + 1;//学号
16                 a[i, 1] = yw;
17                 a[i, 2] = sx;
18                 a[i, 3] = yy;
19                 a[i, 4] = yw + sx + yy;
20
21             }
22
23             //排序
24             for (int i = 0; i <6; i++)
25             {
26                 for (int j = i; j <5; j++)
27                 {
28                     if (a[i,4]<a[j+1,4])
29                     {
30                         int txh = a[j + 1, 0];
31                         a[j + 1, 0] = a[i, 0];
32                         a[i, 0] = txh;
33
34                         int tyw = a[j + 1, 1];
35                         a[j + 1, 1] = a[i, 1];
36                         a[i, 1] = tyw;
37
38                         int tsx = a[j + 1, 2];
39                         a[j + 1, 2] = a[i, 2];
40                         a[i, 2] = tsx;
41
42                         int tyy = a[j + 1, 3];
43                         a[j + 1, 3] = a[i, 3];
44                         a[i, 3] = tyy;
45
46                         int tzf = a[j + 1, 4];
47                         a[j + 1, 4] = a[i, 4];
48                         a[i, 4] = tzf;
49                     }
50                 }
51             }
52             Console.Clear();
53             //输出
54             Console.WriteLine("学号\t语文\t数学\t英语\t总分\t名次");
55             for (int i = 0; i < 6; i++)
56             {
57                 Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",a[i,0],a[i,1],a[i,2],a[i,3],a[i,4],i+1);
58             }
59             int sum1 = 0, sum2 = 0, sum3 = 0;
60             for (int i = 0; i < 6; i++)
61             {
62
63                 sum1 += a[i, 1];
64
65                 sum2 += a[i, 2];
66
67                 sum3 += a[i, 3];
68
69
70             }
71             Console.WriteLine("平均分:{0}\t{1}\t{2}",sum1/6,sum2/6,sum3/6);
72
73         }

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

举例:

 1 static void Main(string[] args)
 2         {
 3         //锯齿数组
 4             int[][] a = new int[3][];
 5             int[] a1 = new int[] {3,4,5,6,7};
 6             int[] a2 = new int[] {1,2,3};
 7             int[] a3 = new int[] {7,8,9,10};
 8
 9             a[0] = a1;
10             a[1] = a2;
11             a[2] = a3;
12
13             //显示
14             for (int i = 0; i <a.Length; i++)//a.length=3
15             {
16                 for (int j = 0; j <a[i].Length; j++)
17                 {
18                     Console.Write(a[i][j]+"\t");
19                 }
20                 Console.Write("\n");
21             }
22
23         }

注意:

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

三.集合:

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

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

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

举例:

 1 static void Main000(string[] args)
 2         {
 3             ArrayList a = new ArrayList();
 4             a.Add(10);
 5             a.Add(20);
 6             a.Add(25);
 7
 8             a.Insert(1, 15);
 9
10             a.RemoveAt(2);
11
12             a[1] = (int)a[1] + 10;
13
14
15             for (int i = 0; i < a.Count; i++)
16             {
17                 Console.WriteLine(a[i]);
18             }
19         }

运行结果:

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

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

取值
a[索引号]

举例

 1  static void Main(string[] args)
 2         {
 3             List<int> a = new List<int>();
 4             a.Add(5);
 5             a.Add(10);
 6             a.Add(20);
 7
 8             a.Insert(2,15);
 9             a.RemoveAt(1);
10
11             a.Sort();
12             a.Reverse();
13
14             for (int i = 0; i < a.Count; i++)
15             {
16                 Console.WriteLine(a[i]);
17             }
18
19         }

运行结果:

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

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

取值:
a[键值]

举例:

 1  static void Main(string[] args)
 2         {
 3             Dictionary<int, string> a = new Dictionary<int, string>();
 4             a.Add(101,"haha");
 5             a.Add(103,"hehe");
 6             a.Add(105,"xixi");
 7             a.Add(107,"哈哈");
 8
 9             a.Remove(103);
10             a[105] = "不许笑";
11             foreach (KeyValuePair<int,string>p in a)
12             {
13                 Console.WriteLine(p.Value);
14             }
15         }

运行结果:

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

 举例:

 1  static void Main(string[] args)
 2         {
 3             Stack<int> a = new Stack<int>();
 4
 5             //向集合里推入元素
 6             a.Push(10);
 7             a.Push(20);
 8             a.Push(30);
 9             a.Push(40);
10             //将元素一个个弹出集合,因为stack 没有索引,所以遵循先进后出原则
11             Console.WriteLine(a.Pop());
12             Console.WriteLine(a.Pop());
13             Console.WriteLine(a.Pop());
14             Console.WriteLine(a.Pop());
15
16         }

运行结果:

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

举例:

 1  static void Main(string[] args)
 2         {
 3             Queue<int> a = new Queue<int>();
 4
 5             //进入队列
 6             a.Enqueue(10);
 7             a.Enqueue(20);
 8             a.Enqueue(30);
 9             a.Enqueue(40);
10
11             //出队列,先进先出
12             Console.WriteLine(a.Dequeue());
13             Console.WriteLine(a.Dequeue());
14             Console.WriteLine(a.Dequeue());
15             Console.WriteLine(a.Dequeue());
16         }

运行结果:

2016.4.23

C# 一维数组、二维数组(矩形数组)、交错数组(锯齿数组)的使用

2017年07月06日 15:35:42

阅读数:856

数组:是一个存储相同类型元素且固定大小的顺序集合。

数组声明:

datatype[] arrayName;

数组类型是从抽象基类型 Array 派生的引用类型。由于此类型实现了 IEnumerable ,因此可以对 C# 中的所有数组使用 foreach 迭代,foreach循环对数组内容进行只读访问,所以不能改变任何元素的值。

//一维数组
        static void Main(string[] args)
        {
            int[] a = { 1,2,3,3}; //初始化
            int[] b = new int[] { 1,2,3,4,5};//初始化
            //var c= a.Intersect(b);  //求交集
            var c = b.Except(a); //求差集
            foreach (var item in c)
            {
                Console.WriteLine(item);
            }

Console.WriteLine("数组A的平均值:"+ a.Average());

Console.Read();
        }

//二维数组 同 多维数组 又称矩形数组
        static void Main(string[] args)
        {
            int[,] myArray;
            myArray = new int[2, 3];//2行 3列
            myArray[0, 0] = 1;
            myArray[0, 1] = 2;
            myArray[0, 2] = 3;

myArray[1, 0] = 4;
            myArray[1, 1] = 5;
            myArray[1, 2] = 6;

foreach (var item in myArray)
            {
                Console.WriteLine(item);
            }

Console.ReadLine();
        }

//锯齿数组 又称交错数组
        static void Main(string[] args)
        {
            //声明一个锯齿数组  三行
            int[][] myArray;
            myArray = new int[3][];

myArray[0] = new int[] { 1,2,3,4,5};
            myArray[1] = new int[3] { 1,4,6};
            myArray[2] = new int[] { 2,3,4,5};

//Console.WriteLine(myArray[1][3]); 第二行,第四个数,不存在,会报错:超出索引

//数据输出如下:
            for (int i = 0; i < myArray.Length; i++)
            {
                Console.WriteLine("第"+(i+1)+"行");
                for (int j = 0; j < myArray[i].Length; j++)
                {
                    Console.Write(myArray[i][j]+",");
                }
                Console.WriteLine();
                
            }

Console.ReadLine();

}

 

C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)

 

数组是变量的索引列表,可以在方括号中指定索引来访问数组中的各个成员,其中索引是一个整数,从0开始。

一维数组
多维数组(矩形数组)
数组的数组(锯齿数组)

数组必须在访问之前初始化,数组的初始化有两种方式,可以以字面的形式指定数组的内容,也可以使用new关键词显式的初始化数组;


1
2
3
int[] arr = { 1, 2, 3 };
 
int[] arr1 = new int[3];

数组类型是从抽象基类型 Array 派生的引用类型。由于此类型实现了 IEnumerable ,因此可以对 C# 中的所有数组使用 foreach 迭代,foreach循环对数组内容进行只读访问,所以不能改变任何元素的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
int[] arr = { 1, 2, 3 };
       foreach (var item in arr)
       {
           Console.WriteLine(item);
       }
       Array arr2 = Array.CreateInstance(typeof(int), 3);
       arr2.SetValue(1, 0);
       arr2.SetValue(2, 1);
       arr2.SetValue(3, 2);
       foreach (var item1 in arr2)
       {
           Console.WriteLine(item1);
       }

多维数组是使用多个索引访问其元素的数组,下面以二维数组为例:


1
2
3
4
5
6
7
8
int[,] arr = { { 1, 2 }, { 4, 5 }, { 7, 8 } };
         int[,] arr1 = new int[3, 2];
         arr1[0, 0] = 1;
         arr1[0, 1] = 2;
         foreach (var item in arr)
         {
             Console.WriteLine(item);
         }

方括号中的第一个数字指定花括号对,第二个数字花括号中的元素

锯齿数组:该数组中的每个元素都是另外一个数组,  每行都有不同的元素个数,


    int[][] a = new int[2][];
            a[0] = new int[3];
            a[1] = new int[4];

            int[][] b = new int[2][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6, 7 } };

            int[][] c = { new int[3] { 1, 2, 3 }, new int[4] { 4, 5, 6, 7 } };

            //锯齿数组的访问
            int[][] a = { new int[3] { 1, 2, 3 }, new int[4] { 4, 5, 6, 7 } };
            foreach (var item in a)
            {
                foreach (var item1 in item)
                {
                    Console.WriteLine(item1);
                }
            }
            Console.WriteLine("----------------------------------");
            for (int i = 0; i < a.Length; i++)
            {
                for (int j = 0; j < a[i].Length; j++)
                {
                    Console.WriteLine(a[i][j]);
                }
            }

锯齿数组的初始化:

  • 数值数组元素的默认值设置为零,而引用元素的默认值设置为 null。

  • 交错数组是数组的数组,因此,它的元素是引用类型,初始化为 null

最新文章

  1. centos7安装svn1.8.16
  2. java并发编程(十七)Executor框架和线程池
  3. Python in Unity
  4. runc start container流程分析
  5. Poj(3259),SPFA,判负环
  6. 黑马程序员——JAVA基础之简述设计模式
  7. 105. Construct Binary Tree from Preorder and Inorder Traversal
  8. poj 1659 Frogs&#39; Neighborhood Havel-Hakimi定理 可简单图定理
  9. bzoj2007
  10. javascript——函数属性和方法
  11. HDU 5009 Paint Pearls (动态规划)
  12. ubuntu 软件管理
  13. .Net基础体系和跨框架开发普及
  14. button样式篇一(ant Design React)
  15. Delegate,Action,Func,匿名方法,匿名委托,事件
  16. fiddler常用操作
  17. tomcat启动时,内存溢出,Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread &quot;main&quot;
  18. Showing a tooltip
  19. 2016&quot;百度之星&quot; - 初赛(Astar Round2B)1003 瞬间移动 组合数学+逆元
  20. Storage 的使用

热门文章

  1. (2)Python 变量和运算符
  2. CF 1009A Game Shopping 【双指针/模拟】
  3. CF 996B World Cup 【找规律/模拟】
  4. Unity防破解 &mdash;&mdash; 重新编译mono
  5. UOJ Rounds
  6. [BZOJ 4057] Kingdoms
  7. AtCoder - 2061 Tree Restoring
  8. POJ 3608 Bridge Across Islands (旋转卡壳)
  9. SQL Server 事务隔离级别的解析
  10. 玩转JavaScript正则表达式