C#上机笔记(第五次上机)

来源:互联网 发布:sql安装后连接不上 编辑:程序博客网 时间:2024/05/16 06:07
各位同学:
    大家好!
    这次上机的主要任务是继续研究方法(Methods)的相关内容,同时掌握数组的使用。
1. 研究scoping,了解以下事实:
    a.C#中的变量有两种生命期:自动(Automatic)生命期和静态(Static)生命期。局部变量有自动生命期,而静态变量有静态生命期。
    b.要改变一个局部变量的值,只能通过两种途径:显式为局部变量赋值,或者将局部变量的引用传递给函数参数。
    c.静态变量本质上类似于C++中的全局变量,特别是公有静态变量更是如此。
    d.静态变量的初始化工作是在类的静态构造函数中完成的。关于静态构造函数的概念和例子在下一次课中给出。
    在类中声明的变量是实例变量.

2. 研究cs092,这是一个在C#程序中使用指针的例子。请完成以下工作。
    a.了解事实:在C#中可以使用指针,但不提倡使用指针,指针只可以在非安全代码块中使用。
    b.可以对类的对象使用指针吗?将项目pointer中的整型变量改成某个类的对象,看看程序是否能通过编译?
不可以
Error1Cannot take the address of, get the size of, or declare a pointer to a managed type ('pointer.A')E:/Work/MS.NET/C#/第五次上机/cs092/cs092/pointer/Program.cs1428pointer
但struct可以
    c.了解事实:用new操作生成一个数据实体时,其返回结果可能是一个引用,也可能是一个值,这取决于new后面跟的数据类型。
class or struct
    d.只能在值类型的变量上使用指针,而不能在类对象上使用指针。这是C#对使用指针的限制。具体例子请参考项目classPtr。
3. 编写一个递归函数,它可以将某个数组中的所有元素反转存储。从中了解递归函数设计的基本要领。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ArrayReverse
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] a= new int[11]{1,2,3,4,5,6,7,8,9,0,11};
  12.             rev(ref a,0);
  13.             for(int i=0;i<a.Length;i++){
  14.                 System.Console.WriteLine(a[i]);
  15.             }
  16.         }
  17.         static void rev(ref int[] a,int index)
  18.         {
  19.             if (index < a.Length/2)
  20.             {
  21.                 int tmp = a[index];
  22.                 a[index] = a[a.Length - index-1];
  23.                 a[a.Length - index-1] = tmp;
  24.                 index++;
  25.                 rev(ref a, index);
  26.             }
  27.         }
  28.     }
  29. }

4. 研究IntArray,完成如下工作:
    a.了解设置数组各元素的值的三种典型方法。
    b.将const int ARRAY_SIZE = 10;中的const去掉,程序是否能正常运行,为什么?
能,because...
    c.这个程序的输出在格式上没有对齐,请修改后使各列对齐。
5. 研究ArrayReferenceTest,完成如下工作:
    a.了解“传引用的引用”的具体含义。凡是传引用必须使用ref关键字。
数组是引用类型
    b.了解:两个数组变量进行相等性比较,是比较两个数组对象的地址是否相同,而不是比较数组存储元素是否相等。
  1.       private void showOutputButton_Click( object sender, 
  2.          System.EventArgs e )
  3.       {
  4.          // create and initialize firstArray
  5.          int[] firstArray = { 1, 2, 3 };
  6.          // copy firstArray reference
  7.          int[] firstArrayCopy = firstArray;
  8.          outputLabel.Text = 
  9.             "Test passing firstArray reference by value";
  10.          outputLabel.Text += "/n/nContents of firstArray " +
  11.             "before calling FirstDouble:/n/t";
  12.          // print contents of firstArray
  13.          for ( int i = 0; i < firstArray.Length; i++ )
  14.             outputLabel.Text += firstArray[ i ] + " ";
  15.          // pass reference firstArray by value to FirstDouble
  16.          FirstDouble( firstArray );
  17.          outputLabel.Text += "/n/nContents of firstArray after " +
  18.             "calling FirstDouble/n/t";
  19.          // print contents of firstArray
  20.          for ( int i = 0; i < firstArray.Length; i++ )
  21.             outputLabel.Text += firstArray[ i ] + " ";
  22.          // test whether reference was changed by FirstDouble
  23.          if ( firstArray == firstArrayCopy )
  24.             outputLabel.Text += 
  25.                "/n/nThe references refer to the same array/n";
  26.          else
  27.             outputLabel.Text += 
  28.                "/n/nThe references refer to different arrays/n";
  29.          // create and initialize secondArray
  30.          int[] secondArray = { 1, 2, 3 };
  31.          // copy secondArray reference
  32.          int[] secondArrayCopy = secondArray;
  33.          outputLabel.Text += "/nTest passing secondArray " +
  34.             "reference by reference";
  35.          outputLabel.Text += "/n/nContents of secondArray " +
  36.             "before calling SecondDouble:/n/t";
  37.          // print contents of secondArray before method call
  38.          for ( int i = 0; i < secondArray.Length; i++ )
  39.             outputLabel.Text += secondArray[ i ] + " ";
  40.          SecondDouble( ref secondArray );
  41.          outputLabel.Text += "/n/nContents of secondArray " +
  42.             "after calling SecondDouble:/n/t";
  43.          // print contents of secondArray after method call
  44.          for ( int i = 0; i < secondArray.Length; i++ )
  45.             outputLabel.Text += secondArray[ i ] + " ";
  46.          // test whether reference was changed by SecondDouble
  47.          if ( secondArray == secondArrayCopy )
  48.             outputLabel.Text += 
  49.                "/n/nThe references refer to the same array/n";
  50.          else
  51.             outputLabel.Text += 
  52.                "/n/nThe references refer to different arrays/n"
  53.         
  54.       } // end method showOutputButton_Click
  55.       // modify elements of array and attempt to modify
  56.       // reference 
  57.       void FirstDouble( int[] array )
  58.       {
  59.          // double each element's value
  60.          for ( int i = 0; i < array.Length; i++ )
  61.             array[ i ] *= 2;
  62.          // create new reference and assign it to array
  63.          array = new int[] { 11, 12, 13 };
  64.       }
  65.       // modify elements of array and change reference array
  66.       // to refer to a new array
  67.       void SecondDouble( ref int[] array )
  68.       {
  69.          // double each element's value
  70.          for ( int i = 0; i < array.Length; i++ )
  71.             array[ i ] *= 2;
  72.          // create new reference and assign it to array
  73.          array = new int[] { 11, 12, 13 };
  74.       }  
    
6. 研究cs099。数组中的每个元素可以通过在中括号中加下标的方式访问。这中形式代表了数组中元素分量的本身,既可以读,也可以写。例如将它们输入函数Swap(),就可以达到交换数组元素的值的目的。
7. 研究cs035,将数组a的定义改成int[,][] a;请为a分配空间,为其每一个分量赋值,并用foreach循环遍历该数组。
int[,] 矩形数组    
int[][] 锯齿数组    
a[i].GetLength(n)   获取第n维的长度GetLength(0)即行数
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace cs035
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[][,] a;
  11.             a = new int[3][,];
  12.             a[0] = new int[1, 2];
  13.             a[1] = new int[2, 2];
  14.             a[2] = new int[2, 3];
  15.             int i, j,k;
  16.             int count=1;
  17.             for(i=0;i<a.Length;i++)
  18.                 for(j=0;j<a[i].GetLength(0);j++)
  19.                     for(k=0;k<a[i].GetLength(1);k++)
  20.                         a[i][j,k]=count++;
  21.             foreach(int[,] b in a)
  22.                 foreach(int c in b)
  23.                     Console.WriteLine("{0:00}",c);
  24.             Console.ReadLine();
  25.         }
  26.     }
  27. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace cs035
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[,][] a;
  11.             a=new int[2,3][];
  12.             int i, j, k;
  13.             int count = 1;
  14.             for(i=0;i<a.GetLength(0);i++)
  15.                 for(j=0;j<a.GetLength(1);j++)
  16.                     a[i,j]=new int[i+j+2];
  17.             for (i = 0; i < a.GetLength(0); i++)
  18.                 for (j = 0; j < a.GetLength(1); j++)
  19.                     for (k = 0; k < a[i, j].Length; k++)
  20.                         a[i, j][k] = count++;
  21.             foreach (int[] b in a)
  22.                 foreach (int c in b)
  23.                     Console.WriteLine("{0:00}",c);
  24.         }
  25.     }
  26. }

8. 研究cs051。了解如下事实:
    a.为时容器的容量可以动态增长,不能使用一般的数组,而是要使用集合类型ArrayList。
    b.ArrayList是一个非泛型容器,它容纳的元素的数据类型是Object,因此可以将任意类型的数据“装入”其中。
    c.ArrayList不是“类型安全的”。与它相对应的泛型容器是List<T>。请参照例子了解该泛型集合的使用方法。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. namespace MixAdd
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             ArrayList a = new ArrayList();
  13.             a.Add(5);
  14.             a.Add("string");
  15.             Console.WriteLine(a[0].GetType().ToString());
  16.             Console.WriteLine(a[1].GetType().ToString());
  17.             Console.WriteLine(a[0]);
  18.             Console.WriteLine(a[1]);
  19.             Console.ReadLine();
  20.         }
  21.     }
  22. }

empList.Capacity  容量初始为4,每次增长翻倍;
ArrayList是非泛型的,上例说明取出时可自动识别其类型

原创粉丝点击