The use of ArrayList is compared with that of List <T>.
You can compile a short program to demonstrate the limitations of non-generic collection classes. This program uses the ArrayList collection class in the. NET Framework base class library.ArrayListIs a collection class that is very convenient to use and can be used to store any reference or value type without modification.
[Csharp]View plaincopy
- // The. NET Framework 1.1 way to create a list:
- System. Collections. ArrayList list1 = new System. Collections. ArrayList ();
- List1.Add (3 );
- List1.add( 105 );
- System. Collections. ArrayList list2 = new System. Collections. ArrayList ();
- List2.Add ("It is raining in Redmond .");
- List2.Add ("It is snowing in the mountains .");
However, this kind of convenience requires a price. AddArrayListAny reference or value type in is implicitly forcibly convertedObject. If the item is of the value type, you must perform the packing operation when adding it to the list, and cancel the packing operation during retrieval. Both forced conversion and packing and unboxing reduce the performance. When circular access is required for large sets, the effect of packing and unboxing is significant.
Another restriction is the lack of compile-time type checks; becauseArrayListAll items are forcibly convertedObjectSo the client code cannot be prevented from performing the following operations during compilation:
[Csharp]View plaincopy
- System. Collections. ArrayList list = new System. Collections. ArrayList ();
- // Add an integer to the list.
- List. Add (3 );
- // Add a string to the list. This will compile, but may cause an error later.
- List. Add ("It is raining in Redmond .");
- Int t = 0;
- // This causes an InvalidCastException to be returned.
- Foreach (int x in list)
- {
- T + = x;
- }
AlthoughIntsCombined in oneArrayListThis method is completely legal and sometimes intentional when creating a heterogeneous set, but it is more likely to generate a programming error and cannot be detected until the runtime.
In C # versions 1.0 and 1.1, you can only write your own type-specific set to avoid the risk of common code in the. NET Framework base class library collection class. Of course, since this class cannot be reused for multiple data types, the advantage of generalization will be lost, and you must rewrite this class for each type to be stored.
ArrayListWhat is really needed for similar classes is that the client code specifies the specific data type to be used by these classes based on each instance. In this way, you no longer need to forcibly convertT: System. ObjectAnd also enables the compiler to perform type checks. In other words,ArrayListA type parameter is required. This is exactly what generics can provide. InN: System. Collections. GenericGeneric namespaceList <T>To add items to a collection, the operation is similar to the following:
[Csharp]View plaincopy
- // The. NET Framework 2.0 way to create a list
- List <int> list1 = new List <int> ();
- // No boxing, no casting:
- List1.Add (3 );
- // Compile-time error:
- // List1.Add ("It is raining in Redmond .");
For client codeArrayListComparedList <T>The only syntax added is Declaration and type parameters in instantiation. Although this slightly adds some encoding complexity, the advantage is that you can create a ratioArrayListA safer and faster list, especially when list items are of the value type.
List <T> and ArrayList which features are better?
List is an interface, ArrayList is an implementation of List, and List is another Implementation of List. In the two tables, the ArrayList is implemented based on arrays, And the ArrayList is implemented based on linked lists. For more information, see Thinking in Java and API documentation.
I thought you asked about Java.
List <T> is the generic type of ArrayList, the Data Type in ArrayList is object, and the Data Type in List <T> is specific. ArrayList is similar to a vector, different data types can be stored in an array (converted to an object ).
Generally, use List <T> whenever possible, because ArrayList must be converted once for access. So the performance of List <T> is better.
Reference: blog.csdn.net/..2.aspx
Array, arraylist, and list <T> in c # and their main scopes
The three are arrays, mainly used to store data.
The difference is that the first two are weak types and there is no type restriction.
LIST <T> is a generic type, also known as a strong type. Its function is to restrict the type.
List <type>. If you define a USER1 class and a USER2 class, then list <USER1>, and then you list <USER1> lu = new list <USER1> ();
Here you have a NEW object, and then lu. add (data); // note that the data here must be of the USER type, in other words, it must be a property of the USER class.
If you have added attributes of the USER2 class or attributes of other classes, an error is returned. It must be attributes of the USER class.
This is the role of a strong type.