asp.net C# array數組操作

來源:互聯網
上載者:User

資料是每一款開發應用程式都會用的,他是一ojb資料類型 哦,下面我們來看看關於.net數組吧,下面來看看一款簡單扼要應用執行個體。

using system;
using system.collections.generic;
using system.text;

namespace funwitharrays
{
class program
{
    static void main(string[] args)
    {
      console.writeline("***** fun with arrays *****");
      simplearrays();
      arrayinitialization();
      arrayofobjects();
      rectmultidimensionalarray();
      jaggedmultidimensionalarray();
      passandreceivearrays();
      systemarrayfunctionality();
      console.readline();
    }

    #region helper methods
    static void simplearrays()
    {
      console.writeline("=> simple array creation.");
      // create and fill an array of 3 integers
      int[] myints = new int[3];
      myints[0] = 100;
      myints[1] = 200;
      myints[2] = 300;

      // now print each value.
      foreach (int i in myints)
        console.writeline(i);
      console.writeline();
    }

    static void arrayinitialization()
    {
      console.writeline("=> array initialization.");
      // array initialization syntax using the new keyword.
      string[] stringarray = new string[] { "one", "two", "three" };
      console.writeline("stringarray has {0} elements", stringarray.length);

      // array initialization syntax without using the new keyword.
      bool[] boolarray = { false, false, true };
      console.writeline("boolarray has {0} elements", boolarray.length);

      // array initialization with new keyword and size.
      int[] intarray = new int[4] { 20, 22, 23, 0 };
      console.writeline("intarray has {0} elements", intarray.length);
      console.writeline();
    }

    static void arrayofobjects()
    {
      console.writeline("=> array of objects.");

      // an array of objects can be anything at all.
      object[] myobjects = new object[4];
      myobjects[0] = 10;
      myobjects[1] = false;
      myobjects[2] = new datetime(1969, 3, 24);
      myobjects[3] = "form & void";

      foreach (object obj in myobjects)
      {
        // print the type and value for each item in array.
        console.writeline("type: {0}, value: {1}", obj.gettype(), obj);
      }
      console.writeline();
    }
    static void rectmultidimensionalarray()
    {
      console.writeline("=> rectangular multidimensional array.");
      // a rectangular md array.
      int[,] mymatrix;
      mymatrix = new int[6, 6];

      // populate (6 * 6) array.
      for (int i = 0; i < 6; i++)
        for (int j = 0; j < 6; j++)
          mymatrix[i, j] = i * j;

      // print (6 * 6) array.
      for (int i = 0; i < 6; i++)
      {
        for (int j = 0; j < 6; j++)
          console.write(mymatrix[i, j] + "t");
        console.writeline();
      }
      console.writeline();
    }

    static void jaggedmultidimensionalarray()
    {
      //鋸齒形多維陣列
      console.writeline("=> jagged multidimensional array.");
      // a jagged md array (i.e., an array of arrays).
      // here we have an array of 5 different arrays.
      int[][] myjagarray = new int[5][];

      // create the jagged array.
      for (int i = 0; i < myjagarray.length; i++)
        myjagarray[i] = new int[i + 7];

      // print each row (remember, each element is defaulted to zero!)
      for (int i = 0; i < 5; i++)
      {
        for (int j = 0; j < myjagarray[i].length; j++)
          console.write(myjagarray[i][j] + " ");
        console.writeline();
      }
      console.writeline();
    }

    static void printarray(int[] myints)
    {
      for (int i = 0; i < myints.length; i++)
        console.writeline("item {0} is {1}", i, myints[i]);
    }

    static string[] getstringarray()
    {
      string[] thestrings = { "hello", "from", "getstringarray" };
      return thestrings;
    }

    static void passandreceivearrays()
    {
      console.writeline("=> arrays as params and return values.");
      int[] ages = { 20, 22, 23, 0 };
      printarray(ages);
      string[] strs = getstringarray();
      foreach (string s in strs)
        console.writeline(s);
      console.writeline();
    }

    static void systemarrayfunctionality()
    {
      console.writeline("=> working with system.array.");
      // initialize items at startup.
      string[] gothicbands = { "tones on tail", "bauhaus", "sisters of mercy" };

      // print out names in declared order.
      console.writeline(" -> here is the array:");
      //數組上界getupperbound
      for (int i = 0; i <= gothicbands.getupperbound(0); i++)
      {
        // print a name
        console.write(gothicbands[i] + " ");
      }
      console.writeline("n");

      // reverse them...
      // 反轉數組
      array.reverse(gothicbands);
      console.writeline(" -> the reversed array");
      // ... and print them.
      for (int i = 0; i <= gothicbands.getupperbound(0); i++)
      {
        // print a name
        console.write(gothicbands[i] + " ");
      }
      console.writeline("n");

      // clear out all but the final member.
      console.writeline(" -> cleared out all but one...");
      array.clear(gothicbands, 1, 2);
      for (int i = 0; i <= gothicbands.getupperbound(0); i++)
      {
        // print a name
        console.write(gothicbands[i] + " ");
      }
      console.writeline();
    }
    #endregion
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.