[英]C# objects in arrayLists
I'm working with ArrayList
in C# and I am wondering how I can add objects to an ArrayList
and then retrieve the values from it? 我正在使用C#中的
ArrayList
,我想知道如何将对象添加到ArrayList
,然后从中检索值?
In short, how can I add, delete, edit and read from an ArrayList
containing objects of classes? 简而言之,如何添加,删除,编辑和读取包含类对象的
ArrayList
?
Thankful for all help! 感谢所有的帮助!
Unless you are in a situation where you must use .NET 1.0/1.1, or need to interact with legacy code that uses ArrayList
- you should really avoid using ArrayLists
in new code. 除非您处于必须使用.NET 1.0 / 1.1或需要与使用
ArrayList
遗留代码进行交互的情况下 - 您应该避免在新代码中使用ArrayLists
。 Use the generic collection type List<>
instead. 请改用通用集合类型
List<>
。
The operations to add, remove, and replace an item in List<T>
are quite straightforward. 添加,删除和替换
List<T>
中的项目的操作非常简单。
Let's say you have some hypothetical type Animal
, instances of which you will store in a list: 假设您有一些假设类型的
Animal
,您将在列表中存储的实例:
Animal dog = new Animal("dog");
Animal cat = new Animal("cat");
List<Animal> animalList = new List<Animal>();
// example of adding items to the list
animalList.Add( dog );
animalList.Add( cat );
// example of removing items form the list
animalList.Remove( cat );
// example of replacing an item at a given position
animalList[0] = new Animal("giraffe");
The public interfaces for List<T>
and ArrayList
are actually quite similar. List<T>
和ArrayList
的公共接口实际上非常相似。 The main difference, is that ArrayList can only store object
references since it was implemented before .NET supported generics. 主要区别在于ArrayList只能存储
object
引用,因为它是在.NET支持的泛型之前实现的。
ArrayList listOfObjects = new ArrayList();
int myAge = 207;
listOfObjects.Add( (object)myAge );
In the example above, you MUST cast types like int
( which are value types in .NET ) to object. 在上面的示例中, 必须投类型,例如
int
( 这是在.NET值类型 )到对象。 This results in a boxing conversion - which copies the int value type to a new location on the heap, and passes it to ArrayList. 这会导致装箱转换 - 将int值类型复制到堆上的新位置,并将其传递给ArrayList。 Boxing conversions , are one of the disadvantages of using
ArrayList
- List<T>
avoids this by virtue of being a generic class . 拳击转换是使用
ArrayList
的缺点之一 - List<T>
通过作为泛型类来避免这种情况。 Another issue is that ArrayList
does not prevent you from mixing different types in the list together. 另一个问题是
ArrayList
不会阻止您将列表中的不同类型混合在一起。 For instance: 例如:
listOfObjects.Add( (object)myAge );
listOfObjects.Add( "Hello World" );
are both allowed. 都是允许的。 However, when accessing elements of an
ArrayList
, you must know what type you are trying to retrieve. 但是,在访问
ArrayList
元素时,您必须知道要尝试检索的类型。 This makes ArrayList
more fragile as a collection type, because the caller must write code to either protect themselves from arbitrary types being stored in the ArrayList
, or else use reflection and runtime type checks to convert the values being stored. 这使得
ArrayList
作为集合类型更加脆弱,因为调用者必须编写代码以保护自己免受存储在ArrayList
中的任意类型的影响,或者使用反射和运行时类型检查来转换存储的值。 List<T>
avoids both of these problems by allowing the compiler to help verify that only appropriate types are stored in the collection (those that match the type parameter T
in List<T>
). List<T>
通过允许编译器帮助验证只有适当的类型存储在集合中(那些与List<T>
中的类型参数T
匹配)来避免这两个问题。
There's a great deal more that could be written about interacting with collections - and in fact there is. 还有很多关于与馆藏互动的文章 - 实际上还有。 Here's a link to just one of many great books on the subject.
这里只是关于这个主题的许多好书之一的链接 。 My advice would be, before you begin writing code in .NET/C#, you should take the time to familiarize yourself with the basic concepts of the C# language and type system - what are reference vs. value types.
我的建议是,在开始用.NET / C#编写代码之前,你应该花时间熟悉C#语言和类型系统的基本概念 - 什么是引用与值类型。 What are primitives.
什么是原始。 What are generics.
什么是泛型。 etc. This will help ensure that when you start writing code, the code does what you need it to do.
这将有助于确保在您开始编写代码时,代码可以满足您的需要。 C# has a sophisticated and rich type system- as well as a vast library of framework classes.
C#有一个复杂而丰富的类型系统 - 以及庞大的框架类库。 It's important to have a good grounding in the core aspects of the language before you get too deep into writing actual code.
在深入编写实际代码之前,有必要在语言的核心方面有一个良好的基础。 Examples like those I show above will only get you so far - and they already introduce numerous language concepts: variables, constructors, generics, boxing conversions, etc.
像我上面展示的那些例子只会让你到目前为止 - 他们已经引入了许多语言概念:变量,构造函数,泛型,拳击转换等。
Firstly, it is better to use a List, and not an ArrayList in C#. 首先,最好使用List,而不是C#中的ArrayList。 For instance, if you want a list of strings:
例如,如果您想要一个字符串列表:
List<String> myList = new List<String>();
or 要么
var myList = new List<String>();
Then the methods will be similar, eg 那么方法将是类似的,例如
myList.Add("bla");
var test = myList[0];
im using similar method but im not able to use it..giving some error like object is field cannot use as type..:/ im wondering I was able to use it in java even with a ArrayList object..im understating it bcz mine object is field but why I cannot use it:( 即时通讯使用类似的方法,但我无法使用它。一些错误像对象是字段不能使用类型..:/我想知道我能够在java中使用它甚至与ArrayList对象..我低估它bcz我对象是字段,但为什么我不能使用它:(
List lists = new List(); List lists = new List();
person p2 = new person("Hammad", "Lahore", 1, 123);
person p3 = new person("adnan", "Lahore", 1, 123);
person p4 = new person("qamar", "Lahore", 1, 123);
lists.Add(p2);
you can do it the same as if u work on a list or a collection (do try use .add method). 你可以像在列表或集合上一样工作(尝试使用.add方法)。
also, you can read this msdn link 另外,你可以阅读这个msdn链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.