您的位置:首页 > 编程语言 > C#

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

2008-11-25 13:04 253 查看
各位同学:    大家好!    这次上机的主要任务是继续研究方法(Methods)的相关内容,同时掌握数组的使用。1. 研究scoping,了解以下事实:    a.C#中的变量有两种生命期:自动(Automatic)生命期和静态(Static)生命期。局部变量有自动生命期,而静态变量有静态生命期。    b.要改变一个局部变量的值,只能通过两种途径:显式为局部变量赋值,或者将局部变量的引用传递给函数参数。    c.静态变量本质上类似于C++中的全局变量,特别是公有静态变量更是如此。    d.静态变量的初始化工作是在类的静态构造函数中完成的。关于静态构造函数的概念和例子在下一次课中给出。    在类中声明的变量是实例变量.
2. 研究cs092,这是一个在C#程序中使用指针的例子。请完成以下工作。    a.了解事实:在C#中可以使用指针,但不提倡使用指针,指针只可以在非安全代码块中使用。    b.可以对类的对象使用指针吗?将项目pointer中的整型变量改成某个类的对象,看看程序是否能通过编译?不可以Error 1 Cannot 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.cs 14 28 pointer但struct可以    c.了解事实:用new操作生成一个数据实体时,其返回结果可能是一个引用,也可能是一个值,这取决于new后面跟的数据类型。class or struct    d.只能在值类型的变量上使用指针,而不能在类对象上使用指针。这是C#对使用指针的限制。具体例子请参考项目classPtr。3. 编写一个递归函数,它可以将某个数组中的所有元素反转存储。从中了解递归函数设计的基本要领。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayReverse
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a= new int[11]{1,2,3,4,5,6,7,8,9,0,11};
            rev(ref a,0);
            for(int i=0;i<a.Length;i++){
                System.Console.WriteLine(a[i]);
            }
        }
        static void rev(ref int[] a,int index)
        {
            if (index < a.Length/2)
            {
                int tmp = a[index];
                a[index] = a[a.Length - index-1];
                a[a.Length - index-1] = tmp;
                index++;
                rev(ref a, index);
            }
        }
    }
}

4. 研究IntArray,完成如下工作:    a.了解设置数组各元素的值的三种典型方法。    b.将const int ARRAY_SIZE = 10;中的const去掉,程序是否能正常运行,为什么?能,because...    c.这个程序的输出在格式上没有对齐,请修改后使各列对齐。5. 研究ArrayReferenceTest,完成如下工作:    a.了解“传引用的引用”的具体含义。凡是传引用必须使用ref关键字。数组是引用类型    b.了解:两个数组变量进行相等性比较,是比较两个数组对象的地址是否相同,而不是比较数组存储元素是否相等。      private void showOutputButton_Click( object sender, 
         System.EventArgs e )
      {
         // create and initialize firstArray
         int[] firstArray = { 1, 2, 3 };

         // copy firstArray reference
         int[] firstArrayCopy = firstArray;

         outputLabel.Text = 
            "Test passing firstArray reference by value";

         outputLabel.Text += "/n/nContents of firstArray " +
            "before calling FirstDouble:/n/t";

         // print contents of firstArray
         for ( int i = 0; i < firstArray.Length; i++ )
            outputLabel.Text += firstArray[ i ] + " ";

         // pass reference firstArray by value to FirstDouble
         FirstDouble( firstArray );

         outputLabel.Text += "/n/nContents of firstArray after " +
            "calling FirstDouble/n/t";

         // print contents of firstArray
         for ( int i = 0; i < firstArray.Length; i++ )
            outputLabel.Text += firstArray[ i ] + " ";

         // test whether reference was changed by FirstDouble
         if ( firstArray == firstArrayCopy )
            outputLabel.Text += 
               "/n/nThe references refer to the same array/n";
         else
            outputLabel.Text += 
               "/n/nThe references refer to different arrays/n";

         // create and initialize secondArray
         int[] secondArray = { 1, 2, 3 };

         // copy secondArray reference
         int[] secondArrayCopy = secondArray;

         outputLabel.Text += "/nTest passing secondArray " +
            "reference by reference";

         outputLabel.Text += "/n/nContents of secondArray " +
            "before calling SecondDouble:/n/t";

         // print contents of secondArray before method call
         for ( int i = 0; i < secondArray.Length; i++ )
            outputLabel.Text += secondArray[ i ] + " ";

         SecondDouble( ref secondArray );

         outputLabel.Text += "/n/nContents of secondArray " +
            "after calling SecondDouble:/n/t";

         // print contents of secondArray after method call
         for ( int i = 0; i < secondArray.Length; i++ )
            outputLabel.Text += secondArray[ i ] + " ";

         // test whether reference was changed by SecondDouble
         if ( secondArray == secondArrayCopy )
            outputLabel.Text += 
               "/n/nThe references refer to the same array/n";
         else
            outputLabel.Text += 
               "/n/nThe references refer to different arrays/n"; 
        
      } // end method showOutputButton_Click

      // modify elements of array and attempt to modify
      // reference 
      void FirstDouble( int[] array )
      {
         // double each element's value
         for ( int i = 0; i < array.Length; i++ )
            array[ i ] *= 2;

         // create new reference and assign it to array
         array = new int[] { 11, 12, 13 };
      }

      // modify elements of array and change reference array
      // to refer to a new array
      void SecondDouble( ref int[] array )
      {
         // double each element's value
         for ( int i = 0; i < array.Length; i++ )
            array[ i ] *= 2;

         // create new reference and assign it to array
         array = new int[] { 11, 12, 13 };
      }  
    6. 研究cs099。数组中的每个元素可以通过在中括号中加下标的方式访问。这中形式代表了数组中元素分量的本身,既可以读,也可以写。例如将它们输入函数Swap(),就可以达到交换数组元素的值的目的。7. 研究cs035,将数组a的定义改成int[,][] a;请为a分配空间,为其每一个分量赋值,并用foreach循环遍历该数组。int[,] 矩形数组    int[][] 锯齿数组    a[i].GetLength(n)   获取第n维的长度GetLength(0)即行数using System;
using System.Collections.Generic;
using System.Text;

namespace cs035
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][,] a;
            a = new int[3][,];
            a[0] = new int[1, 2];
            a[1] = new int[2, 2];
            a[2] = new int[2, 3];
            int i, j,k;
            int count=1;
            for(i=0;i<a.Length;i++)
                for(j=0;j<a[i].GetLength(0);j++)
                    for(k=0;k<a[i].GetLength(1);k++)
                        a[i][j,k]=count++;
            foreach(int[,] b in a)
                foreach(int c in b)
                    Console.WriteLine("{0:00}",c);
            Console.ReadLine();
        }
    }
}

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

namespace cs035
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,][] a;
            a=new int[2,3][];
            int i, j, k;
            int count = 1;
            for(i=0;i<a.GetLength(0);i++)
                for(j=0;j<a.GetLength(1);j++)
                    a[i,j]=new int[i+j+2];
            for (i = 0; i < a.GetLength(0); i++)
                for (j = 0; j < a.GetLength(1); j++)
                    for (k = 0; k < a[i, j].Length; k++)
                        a[i, j][k] = count++;
            foreach (int[] b in a)
                foreach (int c in b)
                    Console.WriteLine("{0:00}",c);
        }
    }
}

8. 研究cs051。了解如下事实:    a.为时容器的容量可以动态增长,不能使用一般的数组,而是要使用集合类型ArrayList。    b.ArrayList是一个非泛型容器,它容纳的元素的数据类型是Object,因此可以将任意类型的数据“装入”其中。    c.ArrayList不是“类型安全的”。与它相对应的泛型容器是List<T>。请参照例子了解该泛型集合的使用方法。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace MixAdd
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList a = new ArrayList();
            a.Add(5);
            a.Add("string");
            Console.WriteLine(a[0].GetType().ToString());
            Console.WriteLine(a[1].GetType().ToString());
            Console.WriteLine(a[0]);
            Console.WriteLine(a[1]);
            Console.ReadLine();
        }
    }
}

empList.Capacity  容量初始为4,每次增长翻倍;
ArrayList是非泛型的,上例说明取出时可自动识别其类型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: