Arraylist, hashtable

Source: Internet
Author: User

The System. Collections. ArrayList class is a special array. By adding and deleting elements, You can dynamically change the length of the array.


Arraylist can automatically change the size, insert, delete, and access elements flexibly, Which is slower than the average array.

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Collections;

Namespace _ 928 rabbitrylist
{
Class Program
{
Static void Main (string [] args)
{

Person p1 = new Person ();
P1.Name = "Tom ";
// Array

ArrayList arrayList = new ArrayList ();

ArrayList. Add ("hello world ");
ArrayList. Add (10 );


ArrayList. Add (p1 );

 

String [] names = {"YQQ", "LXF", "ZL "};
// ArrayList. Add (names );
ArrayList. AddRange (names );
// When deleting the object, it determines whether p2 and the p1 in the set are the same object. If it is the same object, it is deleted; otherwise, it cannot be deleted.


String s1 = new string (new char [] {'A', 'B', 'C '});
String s2 = new string (new char [] {'A', 'B', 'C '});

ArrayList. Add (s1 );
ArrayList. ToArray ();

// ArrayList. Remove (p2 );
// ArrayList. Remove (s2 );

For (int I = 0; I <arrayList. Count; I ++)
{
// ArrayList [I]; obtain each element
}
Person p2 = (Person) arrayList [2];

Console. WriteLine (p2.Name );

# Region MyRegion
// ArrayList. Add (10 );
// ArrayList. Add ("hello world ");
// ArrayList. Add (3.45 );

// ArrayList. Add (10 );
// ArrayList. Add ("hello world ");
// ArrayList. Add (3.45 );

// ArrayList. Add (10 );
// ArrayList. Add ("hello world ");
// ArrayList. Add (3.45 );

// For (int I = 0; I <9; I ++)
//{
// ArrayList. RemoveAt (0 );
//}

// ArrayList. TrimToSize ();

// Console. WriteLine (arrayList. Count); // set the actual number of elements
// Console. WriteLine (arrayList. Capacity );
# Endregion


Console. ReadKey ();

}
}
Public class Person
{
Public string Name
{
Get;
Set;
}
Public int Age
{
Get;
Set;
}
}
}


Display the array elements in this arraylist. If there is the same, only one element is displayed:


Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Collections;

Namespace lianxi2
{
Class Program
{
Static void Main (string [] args)
{
ArrayList arrayList1 = new ArrayList () {"a", "B", "c", "d "};
ArrayList arrayList2 = new ArrayList () {"c", "e", "d", "f "};
ArrayList arrayList3 = new ArrayList ();

ArrayList3.AddRange (arrayList1 );
For (int I = 0; I <arrayList2.Count; I ++)
{
If (! ArrayList3.Contains (arrayList2 [I])
{
ArrayList3.Add (arrayList2 [I]);
}
}

For (int I = 0; I <arrayList3.Count; I ++)
{
Console. WriteLine (arrayList3 [I]);

}

Console. ReadKey ();
}
}
}

 

Hashtable:

In. in the. NET Framework, Hashtable is System. A container provided by the Collections namespace is used to process and present key-value pairs similar to the keyvalue. The key is usually used for quick search, and the key is case sensitive; value is used to store the value corresponding to the key. In Hashtable, keyvalue pairs are of the object type, so Hashtable can support any type of keyvalue pairs.


❤Simple operations on Hash Tables

Add a keyvalue key-value pair in the hash table: HashtableObject. Add (key, value );
Remove a keyvalue key-value pair in the hash table: HashtableObject. Remove (key );
Remove all elements from the hash table: HashtableObject. Clear ();
Determine whether the hash table Contains the specified key: HashtableObject. Contains (key );
The following console contains all the preceding operations:
Using System;
Using System. Collections; this namespace must be introduced when file uses Hashtable.
Class hashtable
{
Public static void Main ()
{
Hashtable ht = new Hashtable (); file to create a Hashtable instance
Ht. Add (E, e); Add a keyvalue pair
Ht. Add (A, );
Ht. Add (C, c );
Ht. Add (B, B );

String s = (string) ht [A];
If (ht. Contains (E) file determines whether the hash table Contains a specific key. The return value is true or false.
Console. WriteLine (the E keyexist );
Ht. Remove (C); Remove a keyvalue pair
Console. WriteLine (ht [A]); Output
Ht. Clear (); remove all elements
Console. WriteLine (ht [A]); file will not have any output here
}
}

❤Traverse a hash table

DictionaryEntry Object is required to traverse a hash table. The Code is as follows:
For (DictionaryEntry de in ht) fileht is a Hashtable instance.
{
Console. WriteLine (de. Key); de. Key corresponds to the key
Console. WriteLine (de. Value); de. Key corresponds to the keyvalue Key-value Pair Value
}

❤Sort hash tables

The definition of sorting hash tables here is to re-arrange the keys in the key-Value Pair according to certain rules, but in fact this definition cannot be implemented, because we cannot re-arrange keys directly in Hashtable, if Hashtable needs to provide output of certain rules, we can adopt a work und:
ArrayList akeys = new ArrayList (ht. Keys); file do not forget to import System. Collections
Akeys. Sort (); files are sorted alphabetically.
For (string skey in akeys)
{
Console. Write (skey + );
Console. WriteLine (ht [skey]); output after sorting
}


 

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.