C# 学习笔记 数组、字符串、值类型变量和引用类型变量

1、为什么要使用数组


        
        static void text1()
        {
         //4个步骤
         int[] scores;          //声明数组
         scores = new int[5];   //分配空间
         scores[0] = 67;
         scores[1] = 89;
         scores[2] = 78;
         scores[3] = 80;
         scores[4] = 75;
         //元素操作
         int avgScore = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4] ) / 5;
         Console.WriteLine($"平均成绩是:{avgScore}");
         Console.WriteLine("******************************");
         //3种情况
         int[] score1 = new int[5] { 67, 89, 78, 80, 75 };
         int[] score2 = new int[] { 67, 89, 78, 80, 75 };//
         int[] score3 = { 67, 89, 78, 80, 75 };
         Console.WriteLine($"1平均成绩是:{score1}");
         Console.WriteLine($"2平均成绩是:{score2}");
         Console.WriteLine($"3平均成绩是:{score3}");
         }
    

2、使用for和foreach循环遍历数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace Array_study
{
   internal class Program
   {
      static void Main(string[] args)
        {
         text2();
        }
               
        //遍历的理解
        //就是把元素从数组或集合中,挨个取出来,供我们使用
        static void text2()
        {   //使用for循环遍历数组
            int[] score1 = new int[5] { 67, 89, 78, 80, 75 };
            int sumScore = 0;
            for (int i = 0; i < score1.Length; i++)
            {
                sumScore += score1[i];
            }
            int avgScore = sumScore / score1.Length;
            Console.WriteLine(avgScore);

        //使用foreach循环遍历数组
        //①同样也是不用关心循环次数
        //②要求循环的对象必须有固定的循环次数
        //对比:while循环
        //①不仅不关心循环次数,也不关心具体什么时候结束,因为结束是根据条件决定的
            int sumScore1 = 0;
            foreach(int score in score1)
            {
               sumScore1 += score;
            }
            int avgScore1 = sumScore / score1.Length;
            Console.WriteLine(avgScore1);
         }         
    }
}

3、字符串的分割、连接和替换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace Array_study
{
   internal class Program
   {
      static void Main(string[] args)
      {
        text3();
      }
       //Split()方法:分割字符串,返回字符串数组。
       //string.join()方法:组合字符串。
      static void text3()
      {
            string data = "AB EF HU OO";//使用空字符分割的字符串
            string[] dataArry = data.Split();//使用空格分割的时候,此方法内部不需要参数。                                             
            string newdata = string.Join("_", dataArry);
            Console.WriteLine(newdata);
            Console.WriteLine("****************************");
            
            string data1 = "AA,BB,CC,DD";
            string[] dataArry1 = data1.Split(',');
            string newdata1 = string.Join("&", dataArry1);
            Console.WriteLine(newdata1);
            Console.WriteLine($"{dataArry1.Length}");
            Console.WriteLine("****************************");
            
            string data2 = "QQ,WW,EE,RR";
            string datainfo = data2.Replace(',', '+');
            Console.WriteLine(datainfo);
            Console.WriteLine($"{datainfo.Length}");
        }         
        
   }
}

 4、值类型和引用类型使用区别和原理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace Array_study
{
   internal class Program
   {
      static void Main(string[] args)
        {
         text1();
        }
      static void text4()
        {
            int wang = 90;
            int zhang = wang;
            Console.WriteLine($"修改前:小王成绩:{wang} 小张成绩:{zhang}");
            zhang += 5;
            Console.WriteLine($"修改后:小王成绩:{wang} 小张成绩:{zhang}");
            Console.WriteLine("********************************************************");
            //基本数据类型在传递变量值时,传递的是变量“副本”
            //而不是变量本身,变量修改后相互没有影响 
            
            int[] score = { 80, 80 };
            Console.WriteLine($"修改前[score]:小王成绩:{score[0]} 小张成绩:{score[1]}");
            Console.WriteLine("********************************************************");
            int[] Newscore = score;
            Newscore[1] = Newscore[1] + 5;
            Console.WriteLine($"修改前[score]:小王成绩:{score[0]} 小张成绩:{score[1]}");
            Console.WriteLine($"修改前[Newscore]:小王成绩:{Newscore[0]} 小张成绩:{Newscore[1]}");
            //引用变量传递时传递的是变量本身(引用/地址/指针)新变量并没有开辟新空间,只是指向了引用变量 
            //相当于给原有变量值,提供了一个新的“别名”。 
            //新变量改变了值,本质上改变的是“被引用变量”本身的值。
            //记住:数组是引用变量!

            string t1 = "常老师";
            
            string t2 = t1;
            t2 = "james";
            Console.WriteLine($"{t1} {t2}");
            Console.WriteLine(t1 + "\t" + t2);
            //字符串在类型划分上属于引用类型,但实际应用效果上是和值类型一样的
        } 
        
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值