开发者

Comparing arraylist c#

in c# if i have an arraylist populated like (ID, ITEMQUANTITY), and i would like to compare them by ID, how would i do that? I mean, i need to customize it so that i can compare it by the first value only, so if i w开发者_JAVA百科ant to insert another item i can check if the id is already in the list.... I know how to do it by looping through all the items in the arraylist, but I remember doing this in Java a while ago with overriding some interface or methods or something...

Currently i'm using SortedList which I can compare by KEY of (KEY, VALUE). But, the problem is, i dont want the items to be sorted... I want it to be so the last one in gets put on the last place. Maybe i can bypass converting to arraylist if i could just set the SortedList not to sort items...

Tnx!

Andrej


Hopefully I understand what you are trying to do correctly. I would always use a generic List<T> instead of an ArrayList.

Create a class to store your data:

class Item {
  public Int32 Id { get; set; }
  public Int32 Quantity { get; set; }
}

You can add new items to a list like this:

var list = new List<Item>();
list.Add(new Item { Id = 1, Quantity = 10 });
list.Add(new Item { Id = 2, Quantity = 20 });

You can check if an item with a specific ID already exists in the list:

var itemWithId2 = list.FirstOrDefault(i => i.Id == 2);
if (itemWithId2 == null)
  list.Add(new Item { Id = 2, Quantity = 20 });

You can get the last item of the list:

var lastItem = list.Last();


Do your object like that:

public class MyObject
{
    public Int32 ID { get; set; }

    public Int32 Quantity { get; set; }

    public override bool Equals(Object obj)
    {
        if (obj == null)
            return false;

        if (obj.GetType() == GetType())
        {
            MyObject tmpObject = obj as MyObject;

            return ID.Equals(tmpObject.ID);
        }

        return false;
    }

    public override int GetHashCode()
    {
        return ID.GetHashCode();
    }
}

Now you can use ArrayList.Contains() together with a lot of other equality methods.

By the way, as all the other guys mentioned, I would also use List<T> instead.


A few approaches, with different pros and cons:

list.Any(item => item.ID == newItem.ID) returns true if there's a matching ID, though this is the same as loop (and indeed slightly more expensive because of the lambda) but cleaner code.

Maintaining a HashSet where the comparator compares on the ID property would mean that new values with an existing ID would not be added.

Maintaining the list in ID order would allow you to quickly either find an existing object in O(log n) time complexity by using BinarySearch(), or else find the location you should insert this new item to maintain the order.

If ID truly identifies the objects (that is to say, when the IDs are the equal the objects should be considered equal) then implementing IEquatable<T> to compare based on ID, overriding object.Equals() to call into that type-specific equality method, and overriding GetHashCode() to return the value of ID (if it is int, or a smaller integeral type, or cast to int if it's uint) or ID's hashcode (if it's a different type) will mean that this becomes the default concept of identity, meaning that HashSet won't need a special comparator, and Contains will do the work for you (note that Contains is essentially a loop too).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜