数组用于存储一组数据。它由连续的内存位置组成。
一些数组的示例包括 −
在三个已排序的数组中查找公共元素using system;class demo { static void commonelements(int []one, int []two, int []three) { int i = 0, j = 0, k = 0; while (i < one.length && j < two.length && k < three.length) { if (one[i] == two[j] && two[j] == three[k]) { console.write(one[i] + " "); i++;j++;k++; } else if (one[i] < two[j]) i++; else if (two[j] < three[k]) j++; else k++; } } public static void main() { int []one = {20, 35, 57, 70}; int []two = {9, 35, 57, 70, 92}; int []three = {25, 35, 55, 57, 67, 70}; console.write("common elements: "); commonelements(one, two, three); }}
在c#中动态创建数组using system;using system.collections;namespace collectionapplication { class program { static void main(string[] args) { arraylist al = new arraylist(); al.add(99); al.add(47); al.add(64); console.writeline("count: {0}", al.count); console.write("list: "); foreach (int i in al) { console.write(i + " "); } console.writeline(); console.readkey(); } }}
使用c#处理不规则数组并访问元素using system;namespace arrayapplication { class myarray { static void main(string[] args) { int[][] points = new int[][]{new int[]{10,5},new int[]{30,40}, new int[]{70,80},new int[]{ 60, 70 }};int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 2; j++) { console.writeline("a[{0}][{1}] = {2}", i, j, points[i][j]); } } // access int x = points[0][1]; console.writeline(x); console.readkey(); } }}
以上就是c# 中数组的好例子有哪些?的详细内容。