【2017-02-28】一维数组、二维数组、多维数组

一、数组

1什么是数组?

一组变量

{0,0,0,0,0,0}

2数组的作用?

操作大量数据

3数组的定义

1)、数组里面的内容必须是同一类型

2)、数据必须有长度限制

二、一维数组

1、定义一个一维数组

数据类型[]变量名 = new 数据类型[长度]

string[] sss = new string[] { "aaa", "bbb", "ccc", "ddd", "eee","fff" };

string[] sss = new string[5] { "aaa", "bbb", "ccc", "ddd", "eee" };

2、一维数组的赋值:

变量名[索引] = 值;

一维数组的取值

 变量 = 变量名[索引]

For(int i=0;i<变量名.Length;i++)

{

   Console.Write(变量名[i]);

}

 

3、作业题

抽奖

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 抽奖
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] name = new string[8] { "张三", "李四", "王二麻子", "狗蛋儿", "葛二蛋", "赵四儿", "二狗子", "三呆子" };
            Random r = new Random();
            int a = r.Next(0, name.Length);



            string[] jp = new string[6] {"新马泰双飞","Mac一台","iphone7 Plus","山地车一辆","餐具一套","水杯" };
            Random s = new Random();
            int b = s.Next(0, jp.Length);


            for (int i = 0; i < name.Length; i++)
            {
                Console.Clear();
                Console.WriteLine(name[i]);
                System.Threading.Thread.Sleep(500);
                if (a == i)
                {
                    Console.Write("恭喜"+name[a]+"中奖!");
                    break;
                }
            }
            Console.ReadLine();
            for (int j = 0; j < jp.Length; j++)
            {
                Console.Clear();
                Console.WriteLine(jp[j]);
                System.Threading.Thread.Sleep(500);
                if (b == j)
                {
                    Console.Write("恭喜"+name[a]+"抽中"+jp[j]+"!");
                    break;
                }
            }
            
            
            Console.ReadLine();
        }
    }
}

三、二维数组

定义:string[,] 名字 = new string[2,3];

//2个一维数组,每一个一维数组中有3个变量

 接赋值 { {"","",""} ,{"","a",""} }

 a的位置:   变量名[1,1]

 

四、三维数组、多维数组

定义:string[,,] 名字 = new string[2,3,4];

//2个二维数组,每一个二维数组中有3个一维数组,每一个一维数组中有4个变量

打印的时候嵌套3for循环,最外边的打印2个二维数组,中间的打印3个一维数组,里面的打印4个变量。方法同二维数组。

原文地址:https://www.cnblogs.com/qq609113043/p/6486491.html