检查对象列表中是否存在对象
Check to see if Object exist in list of Objects
我需要的是我应该用什么代码替换这个:<-- code (exist) -->
。
我有一个 class 可以在 table 中创建单元格。我将这些单元格存储在单元格列表中。
List<Cell> locations = new List<Cell>();
我想查明该列表中是否存在某个单元格。我正在执行以下操作:
cell_in_array(new Cell(x, y));
public bool cell_in_array(Cell cell)
{
if(<-- code (exist) -->){
return true;
} else {
return false
}
}
单元格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;
}
}
您可以使用Any
来判断序列中是否有任何元素满足条件:
locations.Any(c => c.getColumn() == cell.getColumn() && c.getRow() == cell.getRow())
你可以使用 linq 来做到这一点
public bool cell_in_array(Cell cell)
{
return locations.Any(c => c.getColumn() == cell.getColumn() &&
c.getRow() == cell.getRow())
}
Any
判断是否有元素符合条件。
使用 IEqualityComparer<Cell>
的实现让您的生活变得轻松。
我使用这个解决方案是因为也许你会在某处需要那个语义逻辑。
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();
}
}
使用它很简单,如果您检查列表内容,您会看到它包含两个元素,因为第一个和最后一个添加的单元格是相同的。
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));
感谢 HashSet
,他将使用您的 CellComparer
来避免 Cell
重复。
所以使用 auto-属性 而不是 return 字段值的方法。您的单元格 class 必须如下所示:
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 }; }
}
}
您可以将 IEquatable
接口添加到 Cell class:
public class Cell : IEquatable<Cell>
需要 class 中的 bool
方法 Equals
:
public bool Equals(Cell otherCell)
您在其中提供一个代码来判断 this
单元格何时等于 otherCell
(例如,this.getRow() == otherCell.getRow()
等)。
如果 Cell
是 IEquatable
,它允许您在单元格列表上使用 Contains
方法。所以在你的主代码中,你可以使用 locations.Contains(cell)
而不是 <-- code (exist) -->
。
而且您真的应该考虑使用属性重写 Cell
class。
我需要的是我应该用什么代码替换这个:<-- code (exist) -->
。
我有一个 class 可以在 table 中创建单元格。我将这些单元格存储在单元格列表中。
List<Cell> locations = new List<Cell>();
我想查明该列表中是否存在某个单元格。我正在执行以下操作:
cell_in_array(new Cell(x, y));
public bool cell_in_array(Cell cell)
{
if(<-- code (exist) -->){
return true;
} else {
return false
}
}
单元格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;
}
}
您可以使用Any
来判断序列中是否有任何元素满足条件:
locations.Any(c => c.getColumn() == cell.getColumn() && c.getRow() == cell.getRow())
你可以使用 linq 来做到这一点
public bool cell_in_array(Cell cell)
{
return locations.Any(c => c.getColumn() == cell.getColumn() &&
c.getRow() == cell.getRow())
}
Any
判断是否有元素符合条件。
使用 IEqualityComparer<Cell>
的实现让您的生活变得轻松。
我使用这个解决方案是因为也许你会在某处需要那个语义逻辑。
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();
}
}
使用它很简单,如果您检查列表内容,您会看到它包含两个元素,因为第一个和最后一个添加的单元格是相同的。
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));
感谢 HashSet
,他将使用您的 CellComparer
来避免 Cell
重复。
所以使用 auto-属性 而不是 return 字段值的方法。您的单元格 class 必须如下所示:
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 }; }
}
}
您可以将 IEquatable
接口添加到 Cell class:
public class Cell : IEquatable<Cell>
需要 class 中的 bool
方法 Equals
:
public bool Equals(Cell otherCell)
您在其中提供一个代码来判断 this
单元格何时等于 otherCell
(例如,this.getRow() == otherCell.getRow()
等)。
如果 Cell
是 IEquatable
,它允许您在单元格列表上使用 Contains
方法。所以在你的主代码中,你可以使用 locations.Contains(cell)
而不是 <-- code (exist) -->
。
而且您真的应该考虑使用属性重写 Cell
class。