The use of ArrayList is compared with that of List & lt; T & gt ;.

Source: Internet
Author: User

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
  1. // The. NET Framework 1.1 way to create a list:
  2. System. Collections. ArrayList list1 = new System. Collections. ArrayList ();
  3. List1.Add (3 );
  4. List1.add( 105 );
  5. System. Collections. ArrayList list2 = new System. Collections. ArrayList ();
  6. List2.Add ("It is raining in Redmond .");
  7. 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
  1. System. Collections. ArrayList list = new System. Collections. ArrayList ();
  2. // Add an integer to the list.
  3. List. Add (3 );
  4. // Add a string to the list. This will compile, but may cause an error later.
  5. List. Add ("It is raining in Redmond .");
  6. Int t = 0;
  7. // This causes an InvalidCastException to be returned.
  8. Foreach (int x in list)
  9. {
  10. T + = x;
  11. }

 

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
  1. // The. NET Framework 2.0 way to create a list
  2. List <int> list1 = new List <int> ();
  3. // No boxing, no casting:
  4. List1.Add (3 );
  5. // Compile-time error:
  6. // 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.