내가 String[,] matrix
함께하기로 결정 다음console.writeLine (매트릭스) bidimensional 배열 문자열 []
00 node1 node2 node3
node1 0 1 0
node2 1 1 1
node3 0 0 0
같은 행렬을 구축있다. 다음 코드는 내가 원했던 행렬을 얻을 수 있기를 기대했지만 컴파일 할 때 "노드 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();
}
}
}
적어도 노드의 이름을 출력하지 못하는 이유는 무엇입니까? 내가 잘못 찾지 못하는 이유는 무엇입니까? 도움 주셔서 감사합니다.
는 노드 목록에서 내가 문자열이며, 내가 클라이언트 요소와 내 노드와 공급자의 요소를 비교할 수 있도록 linkList는 커넥터 요소를 포함하는 노드의 이름을 얻는다.
2D 배열에는 행이 있고 각 행에는 열이 있으며 각 열에 값이 저장되며 * for 루프 *가 해결할 수없는 것은 없습니다 – Shai
2 차원이 아닌 ArrayList의 ArrayList를 사용하는 이유가 있습니까? 부울 배열? 이름에 대해서는 단순히 두 개의 사전을 추가하여 이름에서 색인을 가져올 수 있습니다. 그러면 많은 양의 메모리가 절약됩니다. –
@Defi 나는 이것을 고려하기 위해 내 의견을 편집했습니다 :-) –