Home > Net >  How to correctly list the contents of a dictionary in c #?
How to correctly list the contents of a dictionary in c #?

Time:06-06

I have a problem with the code below. I read the data from the file. The first line is the number of keys. Everything is fine in the first loop. The lists are being written into the dictionary. I project them on the screen. However, in the second loop, I cannot display the entire contents of the Graf dictionary. I don't know what it's caused by.

The content of the file from which I read the values: 4\n 0 1\n 0 3\n 1 0\n 1 2\n 1 3\n 2 1\n 2 3\n 3 0\n 3 1\n 3 2\n

namespace ConsoleAppBFS

{ class Program { static Dictionary<string, List> Graf = new Dictionary<string, List>(); static List lista = new List(); static void ListOut(List listOut, string key) { foreach (var value in listOut) { Console.Write("\tKey = {0}, Value = {1}", key, value); } } static void Main(string[] args) {

        string[] lines = System.IO.File.ReadAllLines(@"dane.txt");
        string VertNumber = ""; 
        Boolean FirstLine = true;
        string Key = "";
        string Value = "";
        string tmp= "";
        int i = 0;
     
        lista.Clear();

        //first loop
        foreach (string line in lines)
        {
            if (FirstLine)
            {
                VertNumber = line;
                FirstLine = false;
                Console.WriteLine($"\nLiczba wirzechołków: {VertNumber}\n");
            }
            else
            {
                string[] subs = line.Split(' ');
                                   
                Key = subs[0];
                Value = subs[1];
                
                if (Int32.Parse(Key) == i)
                {
                    lista.Add(Value);
                    tmp = Key;
                }
                else
                {
                    Graf.Add(tmp, lista);
                    
                    System.Console.WriteLine("List:");
                    ListOut(Graf[tmp], tmp);
                    
                    Console.WriteLine("\n");

                    lista.Clear();
                    lista.Add(Value);
                    i  ;
                }
            }
        }

        Console.WriteLine("Last List:");
        Graf.Add(tmp, lista);
        ListOut(Graf[tmp], tmp);
        
        Console.WriteLine("\n\nGraf: \n");
        //second loop
        foreach (var key in Graf.Keys)
        {
            ListOut(Graf[key], key);
            Console.WriteLine("\n");
        }
        System.Console.ReadKey();
    }
}

}

CodePudding user response:

However, in the second loop, I cannot display the entire contents of the Graf dictionary. I don't know what it's caused by.

Your assumption that the second loop does not display the entire contents of the Graf dictionary is simply wrong. The second loop will iterate over all keys in the Graf dictionary. The dictionary does not have "hidden" content that is not associated with any key in the dictionary.

So, given that the second loop iterates over every and all keys in the dictionary (it's not forgetting or skipping a single key), and Graf[key] accessing the associated value of each key, there is only one conclusion: Your code does not populate the dictionary correctly. The problem is not with the second loop, but with the first loop that reads the file content and populates the dictionary.

How to fix it? By single-stepping through the code using the debugger, and painstakingly and attentively observing and verifying the content of every variable involved in the line of code that has just been executed and every variable involved in the next line of code being executed next. As/when you wrote the code, you should got a solid idea of what each of the variables should contain at any given step/phase of the program execution. Use the debugger to observe and verify how your program behaves, how the content of the variables in your program change during program execution. If something does not add up with regard to the value of any variables, investigate where that unexpected value is coming from and/or how it has been generated. Step-by-step you will then get an insight of what your code does wrong and how to correct it.

CodePudding user response:

I don't know what are you going to achieve but try this

class Program
{
    static Dictionary<string, List<int>> Graf = new Dictionary<string, List<int>>();

    static void ListOut(List<int> listOut, string key)
    {
        foreach (var value in listOut)
        {
            Console.Write("\tKey = {0}, Value = {1}", key, value);
        }
    }
    static void Main(string[] args)
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\temp\dane.txt");
        string VertNumber = "";
        Boolean FirstLine = true;
        string tmp = "";

        //first loop
        foreach (string line in lines)
        {
            if (FirstLine)
            {
                VertNumber = line;
                FirstLine = false;
                Console.WriteLine($"\nLiczba wirzechołków: {VertNumber}\n");
            }
            else
            {
                string[] subs = line.Split(' ');

                string key = subs[0];
                int value = int.Parse(subs[1]);

                if (!Graf.ContainsKey(key))
                {
                    Graf[key] = new List<int>() { value };
                }
                else
                {
                    Graf[key].Add(value);
                }
            }
        }

        Console.WriteLine("\n\nGraf: \n");
        //second loop
        foreach (var key in Graf.Keys)
        {
            ListOut(Graf[key], key);
            Console.WriteLine("\n");
        }
        System.Console.ReadKey();
    }
}
  • Related