我已经建立了一个如下的矩阵
00 node1 node2 node3
node1 0 1 0
node2 1 1 1
node3 0 0 0
我决定用String[,] matrix
. 我希望下面的代码能给我我想要的矩阵,但是当我编译它时,它只打印出“node i”和“node j”其他任何东西。
public AdjMatrix(ArrayList nodeList,ArrayList linkList)
{
String[,] matrix = new String [nodeList.Count,nodeList.Count];
ArrayList result = new ArrayList();
using (StreamWriter writer = new StreamWriter("C:\\Users\\Martina\\Desktop\\matrix.txt"))
{
Console.SetOut(writer);
//inizializzazione dei nomi delle classi
for (int i = 0; i < nodeList.Count; i++)
{
if (i == 0)
{
matrix[i,0]=("");
}
else
{
foreach (EA.Element node in nodeList)
{
matrix[i,0] = node.Name;
}
Console.WriteLine("la riga della matrice" + matrix[i,0]);
}
}
//inizializzazione dei valori della matrice
for (int j = 0; j < nodeList.Count; j++)
{
if (j==0)
{
matrix[0,j]=("");
}
else
{
foreach (EA.Element node in nodeList)
{
matrix[0,j] = node.Name;
}
Console.WriteLine("la riga della matrice" + matrix[0,j]);
}
}
//definizione dell'esistenza del link
foreach (EA.Connector link in linkList)
{
for (int i = 1; i < nodeList.Count; i++)
{
int supplier = link.SupplierID;
int client = link.ClientID;
String supplierNode = modelRepository.GetElementByID(supplier).Name;
String clientNode = modelRepository.GetElementByID(client).Name;
if (supplierNode.Equals((String)matrix[i,0]))
{
for (int j = 1; j < nodeList.Count; j++)
{
if (clientNode.Equals((String)matrix[0,j]))
{
matrix[i,j] = "1";
}
else
{
matrix[i,j] = "0";
}
}
}
}
}
Console.WriteLine("matrix : ");
for (int i = 0; i < nodeList.Count; i++)
{
for (int j = 0; j < nodeList.Count; j++)
Console.Write(matrix[i, j] + "\t");
Console.WriteLine();
}
}
}
为什么它至少不打印节点的名称,我找不到错误,为什么它不起作用。感谢您的帮助。
在nodeList中,我得到了字符串节点的名称,并且linkList包含连接器元素,因此我可以将客户端元素和供应商元素与我的节点进行比较。