Big Al Ruby Newbie
Big Al Ruby Newbie

Reputation: 834

Check to see if Object exist in list of Objects

What i need is what Code i should replace this: <-- code (exist) --> with.

I have a class that creates cells in a table. I am storing these cells in an list of cells.

List<Cell> locations = new List<Cell>(); 

I am tring to find out if a cell exist in that list. I am doing the following:

cell_in_array(new Cell(x, y));

public bool cell_in_array(Cell cell)
{
    if(<-- code (exist) -->){
      return true;
    } else {
      return false
    }           
}

Cell Class

public class Cell
{
    int x_pos; // column
    int y_pos;// row
    public Cell(int col, int row)
    {
        x_pos = col;
        y_pos = row;
    }

    public int getRow()
    {
        return y_pos;
    }
    public int getColumn()
    {
        return x_pos;
    }
    public int[] position()
    {
        int[] cell_loc = { x_pos, y_pos };
        return cell_loc;
    }
}

Upvotes: 2

Views: 152

Answers (4)

Reza Aghaei
Reza Aghaei

Reputation: 125197

You can use Any to determine whether any element of a sequence satisfies a condition:

locations.Any(c => c.getColumn() == cell.getColumn() && c.getRow() == cell.getRow())

Upvotes: 0

Dano
Dano

Reputation: 254

You can add IEquatable interface to the Cell class:

public class Cell : IEquatable<Cell>

which requires a bool method Equals in the class:

public bool Equals(Cell otherCell)

in which you provide a code to tell, when this Cell is equal to the otherCell (e.g., this.getRow() == otherCell.getRow() etc.).

If Cell is IEquatable, it allows you to use Contains method on lists of Cells. So in your main code, instead of <-- code (exist) --> you can use locations.Contains(cell).

And you should really think about rewriting your Cell class using Properties.

Upvotes: 2

CodeNotFound
CodeNotFound

Reputation: 23200

Make your life easy by using an impelmentation of IEqualityComparer<Cell>. I use this solution because maybe you will need that seme logic somewhere.

public class CellComparer : IEqualityComparer<Cell>
{
    public bool Equals(Cell x, Cell y)
    {
        if (x == null && y == null) return true;

        if (x == null || y == null) return false;

        if (x.Column == y.Column && x.Row == y.Row) return true;

        return false;
    }

    public int GetHashCode(Cell cell)
    {
        int hCode = cell.Column ^ cell.Row;
        return hCode.GetHashCode();
    }
}

To use it it just simple and if you check the list content you will see that it contains two elements because the first and the last added cells are equally the same.

var list = new HashSet<Cell>(new CellComparer());
list.Add(new Cell(0, 1));
list.Add(new Cell(1, 2));
list.Add(new Cell(0, 1));

Say thanks to HashSet who will use your CellComparer to avoid Cell duplication.

So use auto-property instead of method to return a value of a field. Your cell class must looks like this:

public class Cell
{
    public Cell(int col, int row)
    {
        Column = col;
        Row = row;
    }

    public int Row { get; private set; }

    public int Column { get; private set; }

    public int[] Position
    {
        get { return new[] { Column, Row }; }
    }
}

Upvotes: 3

Hamid Pourjam
Hamid Pourjam

Reputation: 20754

you can use linq to do that

public bool cell_in_array(Cell cell)
{
    return locations.Any(c => c.getColumn() == cell.getColumn() && 
                              c.getRow() == cell.getRow())         
} 

Any will determine if any element matches the condition.

Upvotes: 1

Related Questions