c#数组:在 C# 中使用数组
C# 中的数组易于使用,不像它们更棘手的 C++。
在本文中,我们将看一些演示如何使用数组的代码示例。但是在你使用那个数组之前,问问自己你是否可能不需要一个List代替。C# 中的数组是轻量级且直接的,但它们不可调整大小。如果你需要一个可调整大小的数组,你真正想要的是一个列表。
有几种不同的方法来声明和同时初始化一个数组。
你也可以声明一个数组类型的变量,但实际上根本不让它引用任何东西。以下是可能性的示例:
// Declare the array reference but don't initialize it.
int[] values;
// Declare the array reference, initialize it to
// an array able to hold 4 ints; don't put anything
// in the array.
int[] values1 = new int[4];
// Declare the array reference and set it equal
// to an array of four specific ints.
int[] values2 = new int[] { 1, 3, 4, 6 };
// Same as the above, but nicer.
int[] values3 = { 1, 4, 7, 4 };
将对象放入数组
当然,您不会拘泥于声明 int 数组。您可以将任何对象放入 C# 数组中。唯一的问题是您只能将一种类型的对象放入数组中。
实际上——严格来说这不是真的;多态性意味着您可以将任何对象或任何派生对象放入数组中。
正如我们稍后将看到的,这使得数组非常灵活。
让我们看一个例子。
using System;
namespace ConsoleApplication1
{
class Parent
{
// Code and variables go here!
}
class Child : Parent
{
// Child derives from Parent.
}
class Program
{
static int Main(string[] args)
{
Parent parent1 = new Parent();
Parent parent2 = new Parent();
Child child1 = new Child();
Parent[] objects = new Parent[]{parent1, parent2, child1, new Child(), new Parent()};
return 0;
}
}
}
这个小程序声明了一个 Parent 对象数组,并在其中放入了三个 Parent 对象和两个子对象。如您所见,您甚至可以在数组中 使用new关键字。
在 C# 中迭代数组
我们可以使用foreach或只是一个普通的、可信赖的 for循环来遍历数组,就像过去一样。
在下面的程序中,使 Parent 对象有一个称为 Speak() 的虚拟(可覆盖)方法;然后 Child 对象覆盖 speak 方法。然后我们遍历数组并调用每个对象的方法。
using System;
namespace ConsoleApplication1
{
class Parent
{
virtual public void Speak()
{
Console.WriteLine("Do your homework!");
}
}
class Child : Parent
{
override public void Speak()
{
Console.WriteLine("I don't want to!");
}
}
class Program
{
static int Main(string[] args)
{
Parent parent1 = new Parent();
Parent parent2 = new Parent();
Child child1 = new Child();
Parent[] objects = new Parent[]{parent1, parent2, child1, new Child(), new Parent()};
// Iterate over the array with foreach
foreach(Parent obj in objects)
{
obj.Speak();
}
Console.WriteLine("------------");
// Iterate using for
for (int i = 0; i < objects.Length; i++)
{
objects[i].Speak();
}
// Wait for a keypress
Console.ReadKey();
return 0;
}
}
}
Do your homework!
Do your homework!
I don't want to!
I don't want to!
Do your homework!
------------
Do your homework!
Do your homework!
I don't want to!
I don't want to!
Do your homework!
请注意,for循环使用数组的Length属性来知道要进行多少次循环迭代。
多维数组
您可以在 C# 中声明和初始化一个多维数组,如下所示。
int[,] grid = {
{1, 2, 4},
{0, 4, 3},
{1, 6, 4}
};
使用这种语法,所有子数组的长度必须相同。但是,我们可以创建子数组具有不同长度的数组。在 C# 中,这称为锯齿状数组。
做到这一点(以及理解多维数组)的关键是要意识到我们实际上只是在声明一个引用数组,其中每个引用恰好指向一个数组。
因此,如果您有一个多维数组,则第一对索引括号为您提供数组的适当元素(这是一个引用),而第二对为您提供该引用指向的数组的适当元素。
让我们举个例子。
using System;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
// Declare a multidimensional array
int[][] multi = {
new int[]{1, 3, 4},
new int[]{2, 6},
new int[]{0, 4, 6, 3}
};
// We can change the elements of the 'inner' array
// if we want.
multi[1] = new int[] { 9, 9, 4, 9 };
// We can access the elements too of course.
// Let's change that errant '4' above.
multi[1][2] = 9;
// Let's iterate over it.
foreach (int[] subarray in multi)
{
foreach (int element in subarray)
{
Console.Write(element + " ");
}
// Blank line ...
Console.WriteLine();
}
// Wait for a keypress
Console.ReadKey();
return 0;
}
}
}
1 3 4
9 9 9 9
0 4 6 3
…. 当然,您可以在数组中混合子对象和父对象。
对象数组
事实上,由于所有类都派生自对象类(System.Object 的别名),您甚至可以声明包含对象的数组,并且基本上可以将您喜欢的任何东西放入其中。随心所欲。实际上,请尽量避免不必要地这样做。
object[] stuff = { "hello", 4, true, 5.4 };
foreach(object obj in stuff)
{
Console.WriteLine(obj);
}
hello
4
True
5,4
强烈推荐
海量程序代码,编程资源,无论你是小白还是大神研究借鉴别人优秀的源码产品学习成熟的专业技术强势助力帮你提高技巧与技能。在此处获取,给你一个全面升级的机会。只有你更值钱,才能更赚钱
如果你是初级程序员可以研究别人的代码提高技术,如果你喜欢搞网盟或者外包,可以让你快速建站,还等什么赶快关注吧,我们会持续输出相关资源
常见问题FAQ
- 程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
- 请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!