数组基本操作

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

namespace ConsoleApplication3
{    class Program
    {
        static void Main(string[] args)
        {
              int[] scores = new int[5];
            scores[0] = 50;
            scores[1] = 40;
            scores[2] = 60;
            scores[3] = 80;
            scores[4] = 90;

            //输出第一同学的成绩
            printScores(scores); 
            //并将第一位同学成绩排序
            Array.Sort(scores);
           
            foreach (var item in scores)//遍历输出,使用foreach输出
            {
                Console.Write(item+" ");
            }
            for (int i = 0; i < scores.Length; i++)//用for遍历输出
            {
                Console.WriteLine(scores[i]);
            }
            int[] scores2 = new int[5];
            // 把该同学的最后2门课程成绩复制给第2个同学
            Array.Copy(scores, 3, scores2, 3, 2);
            //输出第2同学的成绩
            printScores(scores2); 

            //该同学的所有课程成绩复制给第3个同学
            int[] scores3 = new int[5];
            scores.CopyTo(scores3, 0);
            //输出第3同学的成绩
            printScores(scores3);

            int m = scores[0];//访问第一个元素
            int m2 = scores[4];//访问最后一个元素
            scores.Min();//获取数组中最小值
            scores.Max();//获取数组中最大值
            scores.Sum();//获取数组中元素的和
            int a = scores.Length;//获取数组长度
            Console.ReadKey();  

        }
        static void printScores(int[] scores)
        {
            int len = scores.Length;
            for (int i = 0; i < len; i++)
            {
                Console.WriteLine(scores[i] ); 
            }
           
        }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值