Array Operations in. net

Source: Internet
Author: User
  1. /* ------------------- C # Learning arrays ------------------*/
  2. // =============================== One-dimensional array
  3. // The new operator is used to create an array and initialize the array elements as their default values. In this example, all array elements are initialized to zero.
  4. Int [] intarr1 = new int [5];
  5. Int [] intarr2 = new int [] {1, 2, 3, 4, 5 };
  6. Int [] intarr3 = {1, 2, 3, 4, 5, 6 };
  7. Int [] intarr4;
  8. // Arraylist Dynamic Array
  9. Arraylist al1 = new arraylist ();
  10. // Assign values directly to Arrays
  11. Intarr4 = new int [] {4, 7, 8, 43, 2 };
  12. For (INT I = 0; I <5; I ++)
  13. {
  14. // Assign values to Arrays
  15. Intarr1 [I] = I * 2;
  16. Al1.add (I );
  17. }
  18. // Output the value of the Dynamic Array
  19. Foreach (int I in al1)
  20. {
  21. Common. writeln (I );
  22. }
  23. Foreach (int I in intarr4)
  24. {
  25. Common. writeln (I );
  26. }
  27. // =================== Implicit type array [do not consider the array type]
  28. VaR A = new [] {1, 10,100,100 0}; // int []
  29. VaR B = new [] {"hello", null, "world"}; // string []
  30. // Output as follows
  31. Foreach (var I in)
  32. {// Here, A can be changed to B
  33. Common. writeln (I );
  34. }
  35. // C and D are also called staggered arrays.
  36. VaR c = new []
  37. {
  38. New [] {1, 2, 4 },
  39. New [] {5, 6, 7, 8}
  40. };
  41. VaR d = new []
  42. {
  43. New [] {"Luca", "Mads", "Luke", "Dinesh "},
  44. New [] {"Karen", "Suma", "Frances "}
  45. };
  46. Foreach (var I in D)
  47. {// Here, D can be changed to C
  48. Foreach (var j in I)
  49. {
  50. Common. writeln (j );
  51. }
  52. }
  53. // ============================= Two-dimensional array and multi-dimensional array
  54. // Statement
  55. Int [,] Two = new int [4, 2];
  56. Int [,] Three = new int [4, 2, 3];
  57. // Declare and initialize
  58. Int [,] array2d = new int [,] {1, 2}, {3, 4}, {5, 6}, {7, 8 }};
  59. Int [,] array3d = new int [,] {1, 2}, {4, 3 },{ 5, 6}, {7, 8 }}};
  60. // Outputs two-dimensional arrays, which are similar to 3D arrays.
  61. For (INT I = array2d. getlowerbound (0); I <= array2d. getupperbound (0); I ++)
  62. {
  63. For (Int J = array2d. getlowerbound (1); j <= array2d. getupperbound (1); j ++)
  64. {
  65. // Note the array syntax
  66. Common. writeln (array2d [I, j]);
  67. }
  68. }
  69. // Output a 3D Array
  70. For (INT I = array3d. getlowerbound (0); I <= array3d. getupperbound (0); I ++)
  71. For (Int J = array3d. getlowerbound (1); j <= array3d. getupperbound (1); j ++)
  72. For (int K = array3d. getlowerbound (2); k <= array3d. getupperbound (2); k ++)
  73. // Note the array syntax
  74. Common. writeln (array3d [I, j, k]);
  75. // Initialize the array but do not specify the level:
  76. Int [,] array1 = {1, 2}, {3, 4}, {5, 6}, {7, 8 }};
  77. // Declare first and then initialize
  78. Int [,] array2;
  79. // It must be written as new int [,]
  80. Array2 = new int [,] {1, 2}, {3, 4}, {5, 6}, {7, 8 }};
  81. // ==================================== Jagged array [staggered array]
  82. // The above are all the positive arrays of offsets, and the following is the array of sertices.
  83. // Staggered array: elements are arrays. The dimensions and sizes of elements in the staggered array can be different. An interleaved array is sometimes called an "array" and its elements are of reference type.
  84. // Declare an array with two elements
  85. Int [] [] myarray = new int [3] [];
  86. // Fill Element
  87. Myarray [0] = new int [5] {1, 3, 5, 7, 9 };
  88. Myarray [1] = new int [3] {2, 4, 6 };
  89. Myarray [2] = new int [2] {0, 2 };
  90. For (INT I = 0; I <myarray. length; I ++)
  91. {
  92. Common. writeln ("the" + I + "element is :");
  93. // Note that only the jagged array can be written as myarray [I]. length.
  94. For (Int J = 0; j <myarray [I]. length; j ++)
  95. {
  96. Common. writeln (myarray [I] [J]);
  97. }
  98. }
  99. // Initialize the statement.
  100. Int [] [] array3 = new int [] []
  101. {
  102. New int [] {1, 2, 3, 4, 5 },
  103. New int [] {3, 4, 5 },
  104. New int [] {0}
  105. };
  106. // Stenographer
  107. Int [] [] array4 =
  108. {
  109. New int [] {1, 2, 3}
  110. };

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.