C#:是否可以进行隐式 Arraylist 分配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/87970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: Is Implicit Arraylist assignment possible?
提问by Lyndon
I'd like to populate an arraylist by specifying a list of values just like I would an integer array, but am unsure of how to do so without repeated calls to the "add" method.
我想通过指定一个值列表来填充一个数组列表,就像我在一个整数数组中一样,但我不确定如何在不重复调用“add”方法的情况下这样做。
For example, I want to assign { 1, 2, 3, "string1", "string2" } to an arraylist. I know for other arrays you can make the assignment like:
例如,我想将 { 1, 2, 3, "string1", "string2" } 分配给一个数组列表。我知道对于其他数组,您可以进行如下分配:
int[] IntArray = {1,2,3};
Is there a similar way to do this for an arraylist? I tried the addrange method but the curly brace method doesn't implement the ICollection interface.
是否有类似的方法可以为数组列表执行此操作?我尝试了 addrange 方法,但花括号方法没有实现 ICollection 接口。
采纳答案by Sunny Milenov
Array list has ctor which accepts ICollection, which is implemented by the Array class.
数组列表具有接受 ICollection 的 ctor,它由 Array 类实现。
object[] myArray = new object[] {1,2,3,"string1","string2"};
ArrayList myArrayList = new ArrayList(myArray);
回答by Guvante
Depending on the version of C# you are using, you have different options.
根据您使用的 C# 版本,您有不同的选择。
C# 3.0 has collection initializers, detail at Scott Gu's Blog
C# 3.0 具有集合初始值设定项,Scott Gu 的博客中的详细信息
Here is an example of your problem.
这是您的问题的示例。
ArrayList list = new ArrayList {1,2,3};
And if you are initializing a collection object, most have constructors that take similar components to AddRange, although again as you mentioned this may not be an option.
而且,如果您正在初始化一个集合对象,大多数构造函数都会采用与 AddRange 类似的组件,尽管正如您所提到的,这可能不是一个选项。
回答by Matt Howells
I assume you're not using C# 3.0, which has collection initializers. If you're not bothered about the overhead of creating a temp array, you could do it like this in 1.1/2.0:
我假设您没有使用具有集合初始值设定项的 C# 3.0。如果您不担心创建临时数组的开销,您可以在 1.1/2.0 中这样做:
ArrayList list = new ArrayList(new object[] { 1, 2, 3, "string1", "string2"});
回答by Lyndon
(kind of answering my own question but...)
(有点回答我自己的问题,但是......)
The closest thing I've found to what I want is to make use of the ArrayList.Adapter method:
我发现的最接近我想要的东西是使用 ArrayList.Adapter 方法:
object[] values = { 1, 2, 3, "string1", "string2" };
ArrayList AL = new ArrayList();
AL = ArrayList.Adapter(values);
//or during intialization
ArrayList AL2 = ArrayList.Adapter(values);
This is sufficient for what I need, but I was hoping it could be done in one line without creating the temporary array as someone else had suggested.
这足以满足我的需要,但我希望它可以在一行中完成,而无需像其他人建议的那样创建临时数组。
回答by Guvante
Your comments imply you chose ArrayList because it was the first component you found.
您的评论暗示您选择了 ArrayList,因为它是您找到的第一个组件。
Assuming you are simply looking for a list of integers, this is probably the best way of doing that.
假设您只是在寻找整数列表,这可能是最好的方法。
List<int> list = new List<int>{1,2,3};
And if you are using C# 2.0 (Which has generics, but not collection initializers).
如果您使用的是 C# 2.0(它有泛型,但没有集合初始值设定项)。
List<int> list = new List<int>(new int[] {1, 2, 3});
Although the int[] format may not be correct in older versions, you may have to specify the number of items in the array.
尽管 int[] 格式在旧版本中可能不正确,但您可能必须指定数组中的项目数。