Question

I have build a matrix like the following

00   node1 node2 node3
node1  0    1     0
node2  1    1     1
node3  0    0     0

I decided to do it with String[,] matrix. I hoped that the following code will get me the matrix that i wanted but when i compile it, it print out only "node i" and "node j" anything else.

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();
            }
            }
         }

Why it doesn't print me at least the name of the nodes, i can't find the mistake, why it doesn't work. Thank you for your help.

In the nodeList i get the names of the nodes that are Strings and the linkList contain the Connector elements so i can compare the client elements and the supplier elements with my nodes.

Was it helpful?

Solution

Ok, going through your code again I just realized that what you do and what you want to do are two different things. What you want is an array of arrays. What you have is an array of arrays of arrays...

Look at your code. matrix is an ArrayList and for each node you add a new ArrayList to the matrix. At this point you already have 2 dimension. While adding an ArrayList for every node to matrix you also go through the freshly created ArrayList and add another ArrayList for each node. So in the end you have (n2)+1 ArrayLists where n is the number of nodes. And I'm pretty sure you do not want that :-)

Again I recommend that you stick to a simple bool[][] and get the node names from a dictionary. Or, if bools do not cut it you can use float[][] for a weighted connection matrix.

OTHER TIPS

So if I understand correctly, you have an ArrayList containing ArrayLists. Casting the content of matrix[i] into a string cannot work. Try changing that to matrix[i][0] for starters and tell what happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top