什么是数组
一个变量,用来保存一组相同类型的数据。
为什么要有数组
保存一个学生的成绩: int score=100;
保存全班30个学生的成绩
int score1=98;
int score2=68;
int score3=88;
int score4=77;
..........
int score30=99;
变量只能保存一个值,如果想要保存多个值,需要声明多个变量。
可以通过数组解决问题。
语法
int[] scores = { 90, 92, 83, 74, 65,22,33,44,55,66,77,88,99,34,56 }; 定义并且直接赋值 定义一个数组, 类型是int; 里面保存一组数据
int[] scores = new int[10]; scores[0] = 100; scores[1] = 95; scores[2] = 98; scores[3] = 92; 先声明数组;后给数组赋值
注意
数组的长度一旦定义,是不可改变的
数组只能存放相同类型的数据
数组的下标从 0 开始
操作数组的时候注意数组下标越界问题
数组的操作
向数组中新增一个元素
int[] scores = new int[4]; //给数组的第一个位置 放值 第一个位置(下标为0) scores[0] = 100; scores[1] = 95; scores[2] = 98; scores[3] = 92; //1. 值是输入的 Console.WriteLine("输入要新增的数字"); int num = Convert.ToInt32(Console.ReadLine()); //找空位置 int index = -1; for(int i = 0; i < scores.Length; i++) { if (scores[i] ==0) { index = i; //保存空位置的下标 break; } } //提示 新增成功;新增失败 if (index == -1) { Console.WriteLine("空间已满,新增失败"); } else { scores[index] = num; Console.WriteLine("新增成功"); } for(int i = 0;i < scores.Length;i++) { Console.WriteLine(scores[i]); }
查询元素
查询某个数字 在数组中是否存在。如果存在,提示存在,并且输出下标;如果不存在,提示不存在。
int[] scores = new int[4]; //给数组的第一个位置 放值 第一个位置(下标为0) scores[0] = 100; scores[1] = 95; scores[2] = 98; scores[3] = 92; //1. 值是输入的 Console.WriteLine("输入要新增的数字"); int num = Convert.ToInt32(Console.ReadLine()); //找空位置 int index = -1; for(int i = 0; i < scores.Length; i++) { if (scores[i] ==num) { index = i; //保存空位置的下标 break; } } //提示 新增成功;新增失败 if (index == -1) { Console.WriteLine("要查找的元素不存在,查找失败"); } else { Console.WriteLine("要查找的元素的下标是 "+index); }
修改元素
查询某个数字 在数组中是否存在。如果存在,修改为最新的数字,提示修改成功;如果不存在,提示修改。
思路
先查找,再修改
先找要查找的数据;如果查到了,就修改;否则,提示修改失败。
int[] scores = new int[10]; //给数组的第一个位置 放值 第一个位置(下标为0) scores[0] = 100; scores[1] = 95; scores[2] = 98; scores[3] = 92; //1. 值是输入的 Console.WriteLine("输入要修改的数字"); int num = Convert.ToInt32(Console.ReadLine()); //92 //保存要查找的数字的下标 int index = -1; for(int i = 0; i < scores.Length; i++) { if (scores[i] ==num) { index = i; //保存空位置的下标 break; } } if (index == -1) { Console.WriteLine("要修改的元素不存在,修改失败"); } else { Console.WriteLine("要修改的元素的下标是 "+index); Console.WriteLine("输入最新的值"); int newNum = Convert.ToInt32(Console.ReadLine()); //90 scores[index] = newNum; Console.WriteLine("修改成功"); } for (int i = 0; i < scores.Length; i++) { Console.WriteLine(scores[i]); }
删除元素
int[] scores = new int[4]; //给数组的第一个位置 放值 第一个位置(下标为0) scores[0] = 100; scores[1] = 95; scores[2] = 98; scores[3] = 92;
删除数组中的98。先查找元素,如果不存在,直接提示删除失败;如果存在,则把98 设置为0.
后面的元素往前移动
int[] scores = new int[4];
//给数组的第一个位置 放值 第一个位置(下标为0) scores[0] = 100; scores[1] = 95; scores[2] = 98; scores[3] = 92; int index=-1; //通过遍历查找数组中98的下标将 下边赋值给index
for(int i=;i<scores.length;i++) { if(scores[i]==98) { int index=i; beeak; } } for(int j=index;scores.length;j++) { //将数组中的98上的值被覆盖 scores[j]=scores[j+1] //令数组的最后一个值设置为零 scores[lenght-1]=0; }
大作业
写一个图书借阅系统。 项目启动成功之后,有如下选项
登录(可选)
=================欢迎进入 ***图书借阅系统===============
-
查看所有图书
-
借阅图书
-
归还图书
-
新增图书
-
删除图书
-
退出系统
internal class Program { // 程先的入口 private static void Main(string[] args) { //初始化 //书名 string[] names = { "西游记","丰乳臀","活着", null, null}; //作者名 string[] authors = { "吴承恩","莫言","余华", null, null}; //状态 string[] status = { "未借出","已借出","已借出", null, null}; //日期 string[] dates = { "","01","05", null, null}; //次数 int[] counts = { 0, 1, 2, 0, 0 }; Console.WriteLine("=================欢迎进入 ***图书借阅系统==============="); string con = ""; do { Console.WriteLine("1. 查看所有图书"); Console.WriteLine("2.借阅图书"); Console.WriteLine("3. 归还图书"); Console.WriteLine("4. 新增图书"); Console.WriteLine("请选择(1---6)"); string choice = Console.ReadLine(); switch (choice) { case "1": Console.WriteLine("在这里实现功能1"); Console.WriteLine(" 书名\t作者\t状态\t日期\t次数"); for (int i = 0;i<names.Length;i++) { if (null == names[i]) { break; } Console.Write(names[i] + "\t" + authors[i] + "\t" + status[i] + "\t" + dates[i] + "\t" + counts[i]); Console.WriteLine(); } //====== break; case "2": Console.WriteLine("在这里实现功能2"); //========== break; case "3": Console.WriteLine("在这里实现功能3"); break; case "4": Console.WriteLine("在这里实现新增"); Console.WriteLine("输入书名"); string name =Console.ReadLine(); Console.WriteLine("输入作者"); string author = Console.ReadLine(); bool f = false; //不存在 for (int i = 0; i < names.Length; i++) { if (name.Equals(names[i])) { f = true; break; } } if (f) { Console.WriteLine("要添加的图书存在,新增失败"); } else { int index = -1; for (int i = 0; i < names.Length; i++) { if (null == names[i]) { index = i; break; } } if (index == -1) { Console.WriteLine("空间已满,新增失败"); } else { names[index] = name; status[index] = "未借出"; authors[index] = author; Console.WriteLine("新增成功"); } } break; } Console.WriteLine("输入0继续"); con = Console.ReadLine(); } while ("0".Equals(con)); } }