C# .NET - compare arraylist elements with string array

Asked By munish singla on 18-Jun-12 01:24 AM
Hi,
i have a string array and an arraylist. i want to compare the elements of string array with arraylist. if the element is not present in Arraylist but is present in string array i want to ad it to arraylist.
the code i have is like this:

string[] NewUser = { "su", "user","test" };
        ArrayList alluser = new ArrayList();
        alluser.Add("su");
        alluser.Add("user");
       
          foreach (string presentuser in alluser)
        {
          foreach (string user in NewUser)
          {
            if (user == presentuser)
            {
              continue;

            }
            else
            {
              alluser.Add(user);
            }
          }

        }
        foreach (string a in alluser)
        {
          Console.WriteLine(a);
        }
Jitendra Faye replied to munish singla on 18-Jun-12 01:36 AM
Use this working  code-

private void button1_Click(object sender, EventArgs e)
      {
        ArrayList arrlst = new ArrayList();
        arrlst.Add("9");
        arrlst.Add("2");
        arrlst.Add("1");
        arrlst.Add("8");

       string[] arrstr = new string[] { "9", "2", "1", "8" };
       string[] arrstrNew = arrlst.ToArray(typeof(string)) as string[];


        if (arrstr.Except (arrstrNew).Count()==0 )
        {
          MessageBox.Show("Equal");  
        }
        else
        {
          MessageBox.Show("Now Equal");
        }


      }

Try this and let me know.
Chintan Vaghela replied to munish singla on 18-Jun-12 01:47 AM

Hi Frndz,

 

Functionality:  Compare array and arraylist and added value into arraylist if not exist

 

First  Added string array to List

 

List<string> tempList = NewUser.ToList();

 

Now remove all  contains from list which exist in Arraylist

 

tempList.RemoveAll(x => alluser.Contains(x));

 

After then using foreach loop added remaing contains in alluser Arraylist.

 

 

        foreach (string item in tempList)

        {

          alluser.Add(item);

        }

 

 

Full Logic     :

 

 

 

        ArrayList alluser = new ArrayList();

        alluser.Add("su");

        alluser.Add("user");

 

 

        List<string> tempList = NewUser.ToList();

        tempList.RemoveAll(x => alluser.Contains(x));

 

        foreach (string item in tempList)

        {

          alluser.Add(item);

        }

 

 

 

Hope this helpful!

Thanks

 

 

 

munish singla replied to munish singla on 18-Jun-12 03:11 AM
  string[] newloginuser = { "su", "user", "test" };
        ArrayList existuser = new ArrayList();
        existuser.Add("su");
        existuser.Add("test");
        foreach (string user in newloginuser)
        {
          if (existuser.Contains(user) == false)
          {
            existuser.Add(user);
            Console.WriteLine("tab created"+"  "+user);
          }
        }
munish singla replied to Chintan Vaghela on 18-Jun-12 03:12 AM
thnks
munish singla replied to Jitendra Faye on 18-Jun-12 03:12 AM
thanks
Jitendra Faye replied to munish singla on 18-Jun-12 03:28 AM
You always welcome.
Chintan Vaghela replied to munish singla on 18-Jun-12 04:06 AM
you most welcome :)