【发布时间】:2020-05-22 19:36:05
【问题描述】:
我想在 List 中找到一个 KeyValuePair 元素,但不知道如何通过“
不能将运算符 == 应用于类型的操作数 'System.Collections.Generic.List'
" 错误。当我将方法更改为 _adjacencyMatrix.FindIndex(from.GetNumer()) 然后我得到“
参数 int 不能分配给参数类型 'System 。谓词
" 然后当我将方法更改为 _adjacencyMatrix.IndexOf(MakePair(from,to)) 然后我得到 "
参数类型'System.Collections.Generic.KeyValuePair' 不是 可分配给参数类型'System.Collections.Generic.List
" 错误。我不知道我能做些什么。你们有什么想法吗?我是 C# 的新手,虽然我在 C++ 中工作过。我附上下面的代码。可能应该有数字而不是 numer 但我不想在我的代码中更改它。
using System.Windows.Documents;
namespace GPS
{
public class Vertex
{
private int _numer;
public Vertex(int numer)
{
this._numer = numer;
}
public void Deconstruct()
{
}
public int GetNumer()
{
return this._numer;
}
}
}
using System.Collections.Generic;
using GPS;
namespace GPS
{
public class Edge
{
private float _distance;
private KeyValuePair<Vertex, Vertex> _fromTo;
public Edge(float distance, KeyValuePair<Vertex, Vertex> fromTo)
{
this._distance = distance;
this._fromTo = fromTo;
}
public KeyValuePair<Vertex, Vertex> GetFromTo()
{
return _fromTo;
}
public float GetDistance()
{
return _distance;
}
public void SetDistance(float distance)
{
this._distance = distance;
}
public void Deconstruct()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Configuration;
using System.Windows.Documents;
using GPS;
namespace GPS
{
public class Graph
{
private int _vertexNumber;
private List _egdesMatrix;
private List<List<Vertex>> _adjacencyMatrix;
public Graf(int vertexNumber)
{
this._vertexNumber = vertexNumber;
this._egdesMatrix = new List();
this._adjacencyMatrix = new List<List<Vertex>>();
}
public void Deconstruct()
{
}
public List GetEgdesMatrix()
{
return _egdesMatrix;
}
public bool IsOutOfRange(Vertex from, Vertex to)
{
return from.GetNumer() < 0 || to.GetNumer() < 0 ||
from.GetNumer() >= _vertexNumber || to.GetNumer() >= _iloscWierzcholkow;
}
private KeyValuePair<Vertex, Vertex> MakePair(Vertex from, Vertex to)
{
return new KeyValuePair<Vertex, Vertex>(from, to);
}
public bool IsThereAnEdge(Vertex from, Vertex to)
{
if (IsOutOfRange(from, to) != false) return false;
for (var i = 0; i < _vertexNumber; i++)
{
if (_adjacencyMatrix[from.GetNumer()]==to) ;
{
return true;
}
}
return false;
}
//list.Find(item => item > 20);
public void DodajKrawedz(Vertex from, Vertex to, float odleglosc)
{
if (IsOutOfRange(from, to) == false && IsThereAnEdge(from, to) == false)
{
_egdesMatrix.Add(MakePair(from, to));
}
}
}
}
【问题讨论】:
-
使用
KeyValuePair<Vertex, Vertex>存储from和to值看起来不正确。您可以使用元组或自己的类 -
好的,我已经改了 ;)
标签: c# list keyvaluepair