Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Binary search, sorting and insertion in array list using “Array List” class in VB and C# .net.

Author: Maliha Atteeq
Download Source Code : 977_arraylist.zip

In this simple article you will learn that how we can do binary search, sorting and insertion in array list using “array list” class in VB and c#.net.

 Binary search in array list:

In order to do binary search in an array list, array list class provides us a function named BinarySearch(). It is an overloaded function but I will discuss the most common and most used function in my article. This function takes an argument which is the value that u want to search in array list. Its return type is integer and  it returns index of that specific value.

To demonstrate make a new window application. Drag one button and a text box on from.

Now write the following code on Button click event:

C#:

private void button1_Click(object sender, EventArgs e)

        {

            int val;

            val = Convert.ToInt16(txt_val.Text);

            int temp = obj.BinarySearch(val);

            if (temp==-1)

            {

                MessageBox.Show("your given value is not present in array list");

            }

            else

            {

                MessageBox.Show("your given value is on index" + Convert.ToString(temp));

            }

        }

 

VB:

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim val As Integer

        val = Convert.ToInt16(txt_val.Text)

        Dim temp As Integer = obj.BinarySearch(val)

        If temp = -1 Then

            MessageBox.Show("your given value is not present in array list")

        Else

            MessageBox.Show("your given value is on index" & Convert.ToString(temp))

        End If

    End Sub

Insertion in array list:

In order to insert a value in array list, array list class provides us a function named as insert(). This function takes two arguments as parameters, one is the index and the second one is the value. This function insert that value on your specific index. Its return type is void.

To demonstrate make a new window application. Drag one button and  two  text box on from.

Now write the following code on Button click event:

C#:

private void btn_ins_Click(object sender, EventArgs e)

        {

            int index;

            int value;

            index = Convert.ToInt16(txt_index.Text);

            value = Convert.ToInt16(txt_va.Text);

            if (index > 4 || index < 0)

            {

                MessageBox.Show("invalid index");

            }

            else if(index>0&&index<4)

            {

                obj.Insert(index, value);

                MessageBox.Show("your value is inserted on your specific index");

            }

        }

VB:

Private Sub btn_ins_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim index As Integer

        Dim value As Integer

        index = Convert.ToInt16(txt_index.Text)

        value = Convert.ToInt16(txt_va.Text)

        If index > 4 OrElse index < 0 Then

            MessageBox.Show("invalid index")

        ElseIf index > 0 AndAlso index < 4 Then

            obj.Insert(index, value)

            MessageBox.Show("your value is inserted on your specific index")

        End If

    End Sub

Sort the array list:

In order to sort the array list, array list class provides us a function named as sort(). It is an over loaded function but I will discuss the most common and used method/function in my article. This function takes no argument as parameter and its return type is void. This function sorts the whole list.

To demonstrate make a new window application. Drag one button on from.

Now write the following code on Button click event:

C#:

private void btn_sort_Click(object sender, EventArgs e)

        {

            obj.Sort();

            MessageBox.Show("your list is sorted");

        }

Vb:

Private Sub btn_sort_Click(ByVal sender As Object, ByVal e As EventArgs)

        obj.Sort()

        MessageBox.Show("your list is sorted")

    End Sub

Now write the following code on FORM LOAD event:

C#

        private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP ARRAY LIST APPLICATION";

            obj.Add(0, 10);

            obj.Add(1, 20);

            obj.Add(2, 30);

            obj.Add(3, 40);

            obj.Add(4, 50);

            obj.Add(5, 60);

 

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP ARRAY LIST APPLICATION"

        obj.Add(0, 10)

        obj.Add(1, 20)

        obj.Add(2, 30)

        obj.Add(3, 40)

        obj.Add(4, 50)

        obj.Add(5, 60)

    End Sub

This simple article tells that how we can do binary search, sorting and insertion in array list using “array list” class in Vb and c#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get total number of elements, search a value and reverse the array list using “array list” class in vb and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net