1

I am having problem with List Object values. Object is an object of class which holding two integer number.

like

List<Object> container;
container.Add(new Coordinates(x_axis, y_axis));  // x_axis=200, y_axis=300

I have to add unique coordinates each time, means next time x_axis, y_axis can never be added to list if it is 200, 300 respectively.

How can I check the already exist item in list objects?

Thanks

Brad Fox
  • 685
  • 6
  • 19
rana
  • 145
  • 1
  • 2
  • 8

5 Answers5

3

Use a List<Coordinates> instead.

To check if a set of coordinates exist in the list, you have to loop though the list and compare the property values:

Coordinates point = new Coordinates(200, 300);

if (!container.Any(c => c.x_axis == point.x_axis && c.y_axis = point.y_axis)) {
  container.Add(point);
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • if I use `List` .. there's error `Inconsistent accessibility: field type 'System.Collections.Generic.List' is less accessible than field 'MyNameSpace.MyClass.Container'` – rana Apr 16 '12 at 08:39
  • @rana: Make the class `Coordinates` public, or make the list private. – Guffa Apr 16 '12 at 09:12
3

Override Equals and GetHashCode for your Coordinates class:

public class Coordinates
{
    public Coordinates(int x, int y)
    {
        X = x;
        Y = y;
    }

    public int X { get; private set; }
    public int Y { get; private set; }

    public override bool Equals(object obj)
    {
        if (!(obj is Coordinates))
        {
            return false;
        }
        Coordinates coordinates = (Coordinates)obj;
        return ((coordinates.X == this.X) && (coordinates.Y == this.Y));
    }

    public override int GetHashCode()
    {
        return (X ^ Y);
    }
}

And use generic List or HashSet of your Coordinates objects:

List<Coordinates> container = new List<Coordinates>();
Coordinates coordinates = new Coordinates(x_axis, y_axis);

if (!container.Contains(coordinates)
    container.Add(coordinates);

And with HashSet:

HashSet<Coordinates> container = new HashSet<Coordinates>();
container.Add(new Coordinates(x_axis, y_axis));
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Regarding `GetHashCode` overriding - I did it same way as Microsoft did in `Point` class. – Sergey Berezovskiy Apr 16 '12 at 08:42
  • 1
    True, but it's not the best practice because of the fact that the result is the same when `X = 10, Y = 20` and `X = 20, Y = 10`. Better use the practice described in my post below. – Felix K. Apr 16 '12 at 10:58
  • Agree, xor gives same value for binary `010` and `101`, `101` and `010`, `110` and `001` etc. Thats why I pointed why I use such strange hash code generation. – Sergey Berezovskiy Apr 16 '12 at 11:27
0

You could cast the items you pull out of your container back to Coordinates, then check their x and y values. Or if it inherits from ICombparable, just use the = operator. If it isn't IComparable, you could make your own class, and inherit the IComerable Interface.

    var myCoord = (Coordinates) container(0);

This is all assuming you really don't want to make your container just hold the Coordinates directly.

Edit: Fixed a bunch of typos

Mikey Mouse
  • 2,968
  • 2
  • 26
  • 44
0

You can use LINQ like this:

if (!container.Any(c => (Coordinates)c.x_axis == x_axis &&
                        (Coordinates)c.y_axis == y_axis))
    container.Add(new Coordinates(x_axis, y_axis));
david.s
  • 11,283
  • 6
  • 50
  • 82
0

You better use a HashSet or Dictionary ( If you need to keep them in a specific order ) to ensure that they are really unique.

Then you should override GetHashCode and Equals to check the equality.

Best practice for GetHashCode: What is the best algorithm for an overridden System.Object.GetHashCode?

Implementation

public class Coordinate
{
    public Int32 X { get; set; }
    public Int32 Y { get; set; }

    public override int GetHashCode()
    {
        Int32 hash = 17;
        hash = hash * 23 + X;
        hash = hash * 23 + Y;
        return hash;
    }
    public override Boolean Equals(object obj)
    {
        Coordinate other = obj as Coordinate;
        if (other != null)
        {
            return (other.X == X) && (other.Y == Y);
        }
        return false;
    }
}

Dictionary

Int32 count = 0;
Dictionary<Coordinate, Int32> cords = new Dictionary<Coordinate, Int32>();
// TODO : You need to check if the coordinate exists before adding it
cords.Add(new Coordinate() { X = 10, Y = 20 }, count++);
// To get the coordinates in the right order
var sortedOutput = cords.OrderBy(p => p.Value).Select(p => p.Key);

HashSet

HashSet<Coordinate> cords = new HashSet<Coordinate>();
cords.Add(new Coordinate() { X = 10, Y = 20 });
Community
  • 1
  • 1
Felix K.
  • 6,201
  • 2
  • 38
  • 71