[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

how use binary search to fine element in list then remove this element?

hi,
in c# .net 2008,
suppose I have List (dailySnapshotList ) of this class (DailySnapshot) :
    public partial class Main : Form
    {
        public static List<DailySnapshot> dailySnapshotList = new List<DailySnapshot>();
........
........
........

}

    public class DailySnapshot
    {
        public Bitmap bDailySnapshot = null;
        public string sDailySnapshot = null;
        public DailySnapshot(Bitmap bDailySnapshot, string sDailySnapshot)
        {
            this.bDailySnapshot = bDailySnapshot;
            this.sDailySnapshot = sDailySnapshot;
        }
    }

somewhere in my code, I need to check all object (in type of DailySnapshot) of this list (dailySnapshotList ) if the string parameter (sDailySnapshot ) of the class (DailySnapshot) contained in the list (dailySnapshotList ), so I need to remove this element from the list

so I try this:

                                if (dailySnapshotList.Contains(new DailySnapshot(null, "test")))
                                {
                                }
but its not working ........

a friend of mine told me to use binary search, so how can I use it ??

0
Ameerh24
Asked:
Ameerh24
  • 2
2 Solutions
 
Fernando SotoCommented:
Hi Ameerh24;

Seeming you are using VS 2008 you can use Linq to Object to resolve this issue. See code snippet.

Fernando
// Find a object DailySnapshot with the field sDailySnapshot = Test
// and remove it from the list.
var delElement = (from i in dailySnapshotList
                  where i.sDailySnapshot == "Test"
                  select i).SingleOrDefault();

if ( delElement != null )
     dailySnapshotList.Remove(delElement);

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Also, the reason your original code doesn't work is that you are checking for a particular INSTANCE:

    if (dailySnapshotList.Contains(new DailySnapshot(null, "test")))

Since you passed in a NEW instance it will of course never be found in the List...

Without LINQ, you could iterate over each instance in the List and check equality based on the FIELDS.

Or you could use the Find() method of your List and pass it a Predicate.
0
 
käµfm³d 👽Commented:
Using your existing code, I believe the Contains() method is overloaded to accept a comparer object, which you could code to check your fields, as Idle_Mind suggested.

http://msdn.microsoft.com/en-us/library/system.collections.comparer(VS.71).aspx
0
 
käµfm³d 👽Commented:
Example of comparer in action:
/// <summary>
/// Defintion of the comparer class which implements the IEqualityComparer interface
/// </summary>
class DailySnapshotComparer : IEqualityComparer<DailySnapshot>
{
    #region IEqualityComparer<DailySnapshot> Members

    public bool Equals(DailySnapshot x, DailySnapshot y)
    {
        return x.Name == y.Name;
    }

    public int GetHashCode(DailySnapshot obj)
    {
        return obj.Name.GetHashCode();
    }

    #endregion
}

/// <summary>
/// Console app to demonstrate usage of the new comparer class within the Contains() method.
/// </summary>
class Program
{
    static void Main(string[] args)
    {
        List<DailySnapshot> myList = new List<DailySnapshot>() { new DailySnapshot("test1"), new DailySnapshot("test2"), new DailySnapshot("test3") };

        if (myList.Contains(new DailySnapshot("test2"), new DailySnapshotComparer()))
            Console.WriteLine("Found");
        else
            Console.WriteLine("NOT FOUND");

        Console.ReadKey();
    }
}

/// <summary>
/// My example of you class. It doesn't match yours exactly, obviously, but should give an idea of what Idle_Mind
/// was talking about
/// </summary>
class DailySnapshot
{
    public DailySnapshot(string name)
    {
        this.Name = name;
    }

    public string Name { get; set; }
}

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now