Usage and difference between arraylist and List objects

Source: Internet
Author: User

For example:

The Code is as follows:
String [] S = new string [3];
// Assign a value
S [0] = "";
S [1] = "B ";
S [2] = "C ";
// Modify
S [1] = "B1 ";

However, arrays also have some shortcomings. For example, it is difficult to insert data between two data arrays. In addition, when declaring an array, we must specify the length of the array at the same time. The length of the array is too long, leading to a waste of memory. The array and length are too short, leading to data overflow errors. In this way, if we do not know the length of the array when declaring the array, it will become very tricky.
To address these disadvantages of arrays, The arraylist object is first provided in C # To overcome these disadvantages.

Arraylist

Arraylist is a specialized class provided by. NET Framework for data storage and retrieval. It is part of the namespace system. Collections. Its size is dynamically expanded and reduced according to the data stored in it. Therefore, we do not need to specify the length of an arraylist object when declaring it.
Arraylist inherits the ilist interface, so it can easily add, insert, and remove data. For example:

The Code is as follows:

Arraylist list = new arraylist ();

// Add data
List. Add ("ABC ");
List. Add (1, 123 );
// Modify data
List [2] = 345;
// Remove data
List. removeat (0 );
// Insert data
List. insert (0, "Hello World ");


Get element value

Object value = al [Index]; // Al is an arraylist object. Generally, type conversion is required for value, for example, int n = (INT) value;
Set element values

Al [Index] = value; // Al is an arraylist object, and the index must be smaller than count
Append Element

Int arraylist. Add (object Value) returns the index of the added element.
Insert element

Void arraylist. insert (INT index, object value)
Delete Element

After an element is deleted, the subsequent elements are moved forward, but the capacity does not change.

Void arraylist. Remove (Object OBJ) searches after (index 0) and deletes the first element that is the same as obj.
Void arraylist. removeat (INT index) deletes the element corresponding to the index.
Void arraylist. removerange (INT index, int count) deletes count elements starting from index.
Search Element

Int arraylist. indexof (object Value) searches after (index 0) and returns the index of the first element that is found to be the same as that of obj.
Int arraylist. indexof (object value, int startindex)
Int arraylist. indexof (object value, int startindex, int count)
Int arraylist. lastindexof (object Value) is searched from the back (index 0) and returns the index of the first element found to be the same as that of obj.
Int arraylist. lastindexof (object value, int startinde (www.111cn.net) X)
Int arraylist. lastindexof (object value, int startindex, int count)

As shown in the above example, the arraylist seems to solve all the shortcomings in the array, so it should be perfect. Why does the list appear after C #2.0?
As shown in the above example, in the list, we not only inserted the string "ABC", but also inserted the number 123. In this way, it is allowed to insert different types of data in the arraylist. Because arraylist treats all data inserted into it as the object type. In this way, when we use the data in the arraylist to handle the problem, it is likely that the error of Type Mismatch will be reported, that is, the arraylist is not of type security. Even though we make sure that we are very careful when inserting data, we have inserted the same type of data, but in use, we also need to convert them to the corresponding original type for processing. This results in the packing and unpacking operations, resulting in high performance loss.

Interspersed with the concepts of packing and unpacking:

To put it simply:

Packing: package data of the value type into the instance of the reference type.
For example, assign 123 of the int type value to the object o

The Code is as follows:
Int I = 123;
Object o = (object) I;
Binning: Extracts value types from referenced data.
For example, assign the O value of the object to the int type variable I
Object o = 123;
Int I = (INT) O;

The process of packing and unpacking is very performance-consuming.

Generic list
It is precisely because arraylist has the disadvantages of unsafe types and packing and unpacking, so the concept of generics emerged after C #2.0. The List class is the generic equivalent class of the arraylist class. Most of its usage is similar to that of arraylist, because the list class also inherits the ilist interface. The most important difference is that when declaring a list set, we also need to declare the object type of data in the list set for it.
For example:

The Code is as follows:

List <int> List = new list <int> ();
// Add data
List. Add (1, 123 );
// Modify data
List [0] = 345;
// Remove data
List. removeat (0 );


In the above example, if we insert the string character "Hello World" into the list set, the IDE will report an error and cannot pass the compilation. This avoids the aforementioned type security issues and the performance problems of packing and unpacking.

The Code is as follows:

Console. writeline ("list test :");
// Declare an integer list
List <int> lstest = new list <int> ();
Lstest. Add (7 );
Lstest. Add (5 );
Lstest. Add (1 );
Lstest. Add (3 );
String strtest = "";
// Sort the list
Lstest. Sort ();
// List Traversal
Foreach (int I in lstest)
Strtest + = I. tostring () + "";
// Output after formatting
Console. Write (string. Format ("Out: {0} ncount: {1} n", strtest, lstest. Count ));
// Read the next button to display data on the screen
Console. readkey ();

The output result is as follows:

Program code

List test:
Out: 1 3 5 7
Count: 4

From: http://www.111cn.net/net/160/48564.htm

Usage and difference between arraylist and List objects

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.