Collection ArrayList, Collections Queue and Stack - Object-oriented programming

ArrayList Collection

The ArrayList class is used to add objects that can be retrieved using an indexing operation (starting with 0) or using a foreach loop. The capacity of ArrayList objects is expanded as needed. The following example shows how to use the Add method of the ArrayList class to add objects of different types to a single array and then retrieve each object with a foreach loop:

ArrayList al = new ArrayList ();

a1.Act! ( Hello );

al.Add ( Mnp );

al.Add (5);

al.Add (new FileStream ( delemete & quot ;, FileMode.Create)); Console.WriteLine ( KonneKHHH contains + al.Count + element: "); foreach (object s in al) Console.Write (s.ToString () + );

The result of this method will be as follows:

The collection contains 4 elements:

Hello World 5 System. 10.FileStream

Note that you can initialize collections during creation, as well as arrays. For example:

ArrayList al = new ArrayList () {"Hello", "World", "this", "checkout"};

In an actual application, elements of the same type are usually added to the ArrayList collection. This allows you to call the Sort method to sort objects by implementing the IComparable interface in them. You can also use the Remove method to delete previously added objects, and the Insert method to add items to a specific location specified by the index. This is shown in the example below:

ArrayList al;

al = new ArrayList () {"Hello", "World", "this", "checkout"};

al.Remove ( this is );

al.lnsert (1, our );

al.Sort ();

foreach (object s in al) Console.Write (s.ToString () + );

The result of the work will be the following line:

Our world Hello check

Note that the elements are sorted alphabetically (the standard IComparable implementation for strings) and the word this absent. You can also create your own IComparer implementation to manage the sort order. Then, as the IComparable.CompareTo method controls the default sort order for the class objects, the IComparer.Compare method can be used to specify special sorting methods. For example, consider a simple class that only implements the IComparer interface:

public class reverseSort: IComparer {

int IComparer.Compare (Object x, Object y) {

return ((new CaselnsensitiveComparer ()). Compare (y, x));

}}

With this class, you can pass an instance of this class to the Sort method of the ArrayList class. The following example shows how you can do this. In this example, the AddRange () method is used to add an array of elements to the ArrayList collection (if there are individual elements, and the array does not yet exist, it can be created and initialized at once):

ArrayList al = new ArrayList ();

al.AddRange (new string [] {"Hello", "world", "this", "check"); al.Sort (new reverseSort ()); // pass its IComparer foreach (object s in al) Console.Write (s.ToString () + ";);

This code will return the following result: this is a test Hello world

To get the reverse order of elements in the ArrayList, use the ArrayList.Reverse () method. To search for a particular element in the ArrayList, you need to call the BinarySearch method and pass it an instance of the object that you are looking for. The BinarySearch method returns the index of the found object. If the object is not found, the result is -1. The following example prints the value 2, since the string this is in the third position, and the first position has the value 0:

ArrayList al = new ArrayList ();

al.AddRange (new string () {"Hello", "World", "this", "check"); Console.WriteLine (al.BinarySearch ( this ));

Similarly, the Contains () method of the ArrayList class returns true if the ArrayList collection contains the specified object, and false if it is not in the collection.

Queue and Stack Collections

The Queue (String) and Stack (Stack) classes store objects that can be retrieved and deleted in one step. Queues use the FIFO processing order, and Stack uses the LIFO processing sequence.

The Queue class uses the Enqueue and Dequeue methods to add and remove objects, and the Stack class uses the Push and Pop methods for this.

The following example shows the difference between classes:

Queue q = new Queue (); q.Enqueue ( Hello ); q.Enqueue ( world ); q.Enqueue ( just testing );

Console.WriteLine ( Using Queue: );

for (int i = 1; i & lt; = 3; i ++) Console.WriteLine (q.Dequeue (). ToString ());

Stack s = new StackQ;

s.Push ( Hello );

s.Push ( world );

s.Push ( just testing );

Console.WriteLine ( Using Stack: );

for (int i = 1; i & lt; = 3; i ++) Console.WriteLine (s.Pop (). ToString ());

This example produces the following result:

Using Queue:

Hello

world

just testing

Using Stack:

just testing

world

Hello

You can use the Queue.Peek () and Stack.Peek () methods to retrieve objects from collections without deleting them. The Clear () method is used in all collections to delete all objects.

thematic pictures

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)