Chapter 5. Collection---(ArrayList, Stack, queue, hash table)

Source: Internet
Author: User

 //ArrayList Collection//Definition:ArrayList Al =NewArrayList (); //add element: Append to the end of the collectionAl. ADD (1); Al. ADD (2); Al. ADD (3); Al. ADD (4); Al. ADD (5); Console.WriteLine ("the elements in the first position are:"+al[0]); //inserting elementsAl. Insert (0,9);//Insert 9 at the position of index 0, followed by the back dataConsole.WriteLine ("after inserting 9, the element in the first position is:"+al[0]); //remove ElementAl. Remove (4);//Remove the first occurrence of this elementConsole.WriteLine ("remove the collection after element 4:"); foreach(ObjectAinchal)            {Console.WriteLine (a); } al. RemoveAt (4);//Remove the element from this index valueConsole.WriteLine ("Remove the collection after the index value is 4:"); foreach(ObjectAinchal)            {Console.WriteLine (a); }            //get the number of collection elementsConsole.WriteLine ("The number of elements in the collection is:"+al.            Count); //determines whether the data in parentheses is returned with a bool value (TRUE or false)            BOOLb = al. Contains (3); Console.WriteLine ("whether there is element 3 in the collection:"+b); //automatic sorting, ascendingal.            Sort (); Console.WriteLine ("the collection after ascending order:"); foreach(ObjectAinchal)            {Console.WriteLine (a); }            //flips the collection, usually after sort, and turns it from big to smallal.            Reverse (); Console.WriteLine ("the collection after flipping:"); foreach(ObjectAinchal)            {Console.WriteLine (a); }            //clone an identical set.ArrayList AAL =NewArrayList (); AAL=(ArrayList) al.            Clone (); Console.WriteLine ("the collection after cloning:"); foreach(ObjectAinchAAL)            {Console.WriteLine (a); }            //emptying the collectional.            Clear (); Console.ReadLine ();

 //Stack collection: Stack set, Advanced, one-by-one value assignment//How to define:Stack AA =NewStack (); //. Push: Pushing elements within the collectionCal Push (1); Cal Push (2); Cal Push (3); Cal Push (4); Cal Push (5); //. Count: Gets the number of elements in the collectionConsole.WriteLine ("number of elements in the collection:"+AA.            Count); //pop-up element, there is no such element in the collectionConsole.WriteLine ("eject the element and remove:"+AA.            Pop ()); //take a look and do not removeConsole.WriteLine ("eject element, do not remove:"+AA.            Peek ()); //iterating through the collectionConsole.WriteLine ("To traverse a collection:"); foreach(ObjectAaainchAA)            {Console.WriteLine (AAA); }            //CloningStack BB =NewStack (); BB=(Stack) AA.            Clone (); Console.WriteLine ("the collection after cloning:"); foreach(ObjectAaainchBB)             {Console.WriteLine (AAA); }            //emptying the collectionAA.            Clear (); Console.ReadLine (); 

//Queue Queues Collection: FIFO, assign values individually, value//How to defineQueue que=NewQueue (); //Add DataQue. Enqueue (1); Que. Enqueue (2); Que. Enqueue (3); Que. Enqueue (4); Que. Enqueue (5); Que. Enqueue (6); //Removing a dataConsole.WriteLine ("Remove the first data:"+Que.            Dequeue ()); //take out the dataConsole.WriteLine ("only take out the first data not removed:"+Que.            Peek ()); //iterating through the collectionConsole.WriteLine ("To traverse a collection:"); foreach(ObjectAinchque)            {Console.WriteLine (a); } console.readline ();

//Hash Set: Advanced, one-by-one assignment, with valueHashtable HT =NewHashtable (); Ht. ADD (0,"Zhang San"); Ht. ADD (1,"John Doe"); Ht. ADD (2,"Harry"); Ht. ADD (3,"Zhao Liu"); Ht. ADD (4,"Feng Qi"); //Removing DataHt. Remove (2); //determine if it containsConsole.WriteLine ("whether it contains 2:"+ht. Contains (2)); //Traversal collection: Reverse print, similar to stack collectionConsole.WriteLine ("traversal collection, key value:"); foreach(intIinchht.            Keys) {Console.WriteLine (i); } Console.WriteLine ("iterate through the collection, value values:"); foreach(stringSinchht.            Values) {Console.WriteLine (s);            } console.readline (); //use an enumeration type to read all the data in a collection, arranged like a tableIDictionaryEnumerator ID =ht.            GetEnumerator (); //get inside each key value value//loop Print: Moves to the next key value, valueConsole.WriteLine ("to iterate over a collection, enumerate:");  while(ID. MoveNext ()) {Console.WriteLine (ID). Key+"\ t"+ID.            Value);  } console.readline (); 

Chapter 5. Collection---(ArrayList, Stack, queue, hash table)

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: 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.