数组(结合下面所给可以简易完成作业)

什么是数组

一个变量,用来保存一组相同类型的数据。

为什么要有数组

保存一个学生的成绩: 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;
}
​

大作业

写一个图书借阅系统。 项目启动成功之后,有如下选项

登录(可选)

=================欢迎进入 ***图书借阅系统===============

  1. 查看所有图书

  2. 借阅图书

  3. 归还图书

  4. 新增图书

  5. 删除图书

  6. 退出系统

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));
​
​
​
​
    }
​
}
  • 28
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,长数组不能直接赋值给短数组。如果你尝试这样做,编译器会报错,提示类型不兼容。这是因为长数组可以存储的元素数量比短数组多,如果直接将长数组赋值给短数组,就可能会导致数据丢失。 例如,如果你有一个长数组longArray,其中存储了10个long类型的元素,如果将其赋值给一个短数组shortArray,其中只能存储4个short类型的元素,那么就会丢失一些数据。 不过,你可以通过显式地转换来将长数组转换为短数组,然后再进行赋值。这种情况下,你需要确保转换过程中不会丢失任何数据,否则可能会导致精度问题。 下面是一个示例代码,演示如何将长数组转换为短数组: ``` long[] longArray = {1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L}; short[] shortArray = new short[4]; for (int i = 0; i < shortArray.length; i++) { shortArray[i] = (short) longArray[i]; } ``` 在这个例子中,我们首先定义了一个长数组longArray,其中存储了10个long类型的元素。然后我们创建一个短数组shortArray,其中可以存储4个short类型的元素。 接下来,我们使用一个for循环,将longArray中的前4个元素转换为short类型,然后赋值给shortArray中的对应位置。在转换时,我们使用了强制类型转换符将long类型转换为short类型,以确保转换后不会丢失数据。 需要注意的是,在进行数组转换时,你需要仔细考虑数据类型和数据长度,以确保不会出现数据丢失或精度问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值