我建立像下面console.writeLine(矩阵)二维阵列字符串[,]
00 node1 node2 node3
node1 0 1 0
node2 1 1 1
node3 0 0 0
我决定String[,] matrix
做的矩阵。我希望下面的代码能够得到我想要的矩阵,但是当我编译它时,它只打印出“节点i”和“节点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();
}
}
}
为什么它不打印我至少节点的名称,我找不到错误,为什么它不起作用。 谢谢你的帮助。
在节点列表我得到的字符串和链表包含连接器元件,这样我可以比较客户端的元素,并与我的节点的供应商元素节点的名称。
二维数组有行,每行有列,每列包含一个值,没有任何两个* for循环*不能解决 – Shai
是否有您所使用的ArrayList的一个ArrayList,而不仅仅是一个二维的理由布尔阵列?对于名称,您可以简单地添加两个字典以从名称中获取索引。这应该可以节省大量的内存。 –
@Defi我编辑了我的评论以考虑到这一点:-) –