Autor Beitrag
dubstep
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 72

Win XP, Win 7
C# (VS 2010)
BeitragVerfasst: Mi 27.04.11 16:39 
Ich habe schon beinahe alle Kombinationen durch, aber leider finde ich den Fehler nicht. :?

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
Dictionary<int,int[]> Adjazenzliste = new Dictionary<intint[]>();
            Adjazenzliste.Add(001new int[]{015030});
            
            foreach(int[] aa in Adjazenzliste.Values)
            {
                textBox4.AppendText(Convert.ToString(aa));
            }

Ausgabe: System.Int32[]

Nimmt man kein Array oder List<T> als Value beim Dictionary, so funktioniert das Auslesen ohne Probleme ... leider wird aber eine solche Datenstruktur benötigt.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
   Dictionary<int,int> Adjazenzliste = new Dictionary<intint>();
            Adjazenzliste.Add(001030);
            

            foreach(int aa in Adjazenzliste.Values)
            {
                textBox4.AppendText(Convert.ToString(aa));
            }

Ausgabe: 30

Vielen Dank für eure Hilfe!
bakachan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 503
Erhaltene Danke: 34

W7 (x64) Ultimate
C# / VB.NET (VS2010 Ultimate)
BeitragVerfasst: Mi 27.04.11 16:56 
Ein Integer-Array kannst du nicht einfach in einen String umwandeln aber du kannst ja auch das Array durchlaufen und die einzelnen Werte verwenden.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
foreach(int[] aa in Adjazenzliste.Values)
  foreach(int value in aa)
  {
    textBox4.AppendText(value.ToString());
  }
dubstep Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 72

Win XP, Win 7
C# (VS 2010)
BeitragVerfasst: Mi 27.04.11 21:25 
Danke! :lol:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
            Dictionary<int,int[]> Adjazenzliste = new Dictionary<intint[]>();
            Adjazenzliste.Add(001new int[] {000010020030});
            

            foreach(int[] aa in Adjazenzliste.Values)
            {
                for (int i = 0; i < 4; i++) 
                {
                    textBox4.AppendText((Convert.ToString(aa[i])) + "\n");
                }
            }


Gibt es eine Möglichkeit, die 000 nicht als 0 darzustellen, sondern mit den ganzen drei Ziffern (also Darstellung als 000)?
Dr. Hallo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110
Erhaltene Danke: 13

XP
C/C++/C#
BeitragVerfasst: Mi 27.04.11 23:52 
so vielleicht?
aa[i].ToString("000")
dubstep Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 72

Win XP, Win 7
C# (VS 2010)
BeitragVerfasst: Do 28.04.11 00:36 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
Adjazenzliste.Add(Convert.ToInt32(textBoxGhost.Text.Substring(03)), new int[] { 100101});
Adjazenzliste.Add(Convert.ToInt32(textBoxGhost.Text.Substring(33)), new int[] { 100101201301});

 foreach(int[] aa in Adjazenzliste.Values)
            {
                for (int i = 0; i < 4; i++)
                {
                    textBox4.AppendText((Convert.ToString(aa[i])) + "\n");
                }
            }

Durch das Durchlaufen des Arrays mit dieser zusätzlichen for-Schleife benötige ich den Index "i", welcher die Größe des jeweiligen Arrays verlangt. Genau dieser Index bereitet mir nun Kopfzerbrechnen, da ich Arrays unterschiedlicher Länge (siehe oben - z.B. einmal ein Array mit Länge 2 und ein Array mit Länge 4) habe. Die Funktion ".Length" darf hier leider nicht zur Anwendung kommen. Im Prinzip soll der Index i genau so viele Zähler haben, wie das Array (man hat ja mehrere Arrays mit unterschiedlicher Länge) lange ist. :(
Bin für jeden Vorschlag dankbar ...!
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 28.04.11 07:58 
Ohne die Länge des Arrays abzufragen kannst du nicht alle Indizes durchgehen, weil du ganz einfach nicht weißt was "alle" sind.

Was hast du denn gegen die vorgeschlagene Lösung mit einer weiteren for..in Schleife?
bakachan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 503
Erhaltene Danke: 34

W7 (x64) Ultimate
C# / VB.NET (VS2010 Ultimate)
BeitragVerfasst: Do 28.04.11 08:00 
Dafür hast du nur 2 Möglichkeiten die hier beide schon genannt wurden.
Eine hast du selbst genannt "Length" und die andere hast du in meinem Beispiel gesehen "foreach".
Dr. Hallo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110
Erhaltene Danke: 13

XP
C/C++/C#
BeitragVerfasst: Do 28.04.11 13:24 
wenn du auf dein i nicht verzichten darfst, kannst du den indexfehler mit try catch abfangen und mit break die innere schleife verlassen. was aber eher die rabiate methode ist.
dubstep Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 72

Win XP, Win 7
C# (VS 2010)
BeitragVerfasst: Fr 29.04.11 01:53 
Mit diesem Code ist es mir möglich, die Länge des Arrays zu erfragen - das klappt:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
            foreach (int[] aa in Adjazenzliste.Values)
            {
                if (aa.Length == 1)
                {
                    textBox5.AppendText("1");
                }

                    if (aa.Length == 3)
                    {
                        textBox5.AppendText("3");
                    }

                    if (aa.Length == 2)
                    {
                        textBox5.AppendText("2");
                    }

                    if (aa.Length == 4)
                    {
                        textBox5.AppendText("4");
                    }
            }


Leider ist jeder Versuch das nun zu integrieren gescheitert: Hier erhalte ich zwar eine Ausgabe, diese ist aber nicht richtig ...

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
           foreach (KeyValuePair<intint[]> cc in Adjazenzliste)
                {
                    textBox4.AppendText(Convert.ToString(cc.Key) + ": ");

                    foreach (int[] aa in Adjazenzliste.Values)
                    {
                        for (int j = 0; j < aa.Length-1; j++)
                        {
                            textBox4.AppendText(Convert.ToString(cc.Value[j] + "; "));
                        }
                        textBox4.AppendText("\n");
                    }
                }
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 29.04.11 05:58 
Da standen doch schon fertige Vorschläge, ich verstehe nicht wo jetzt das Problem liegt?

Bei dir jedenfalls passt da was nicht zusammen:
user profile icondubstep hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
           foreach (KeyValuePair<intint[]> cc in Adjazenzliste)
                {
                    textBox4.AppendText(Convert.ToString(cc.Key) + ": ");

                    foreach (int[] aa in Adjazenzliste.Values)
                    {
                        for (int j = 0; j < aa.Length-1; j++)
                        {
                            textBox4.AppendText(Convert.ToString(cc.Value[j] + "; "));
                        }
                        textBox4.AppendText("\n");
                    }
                }
:?!?:
Außerdem bist du wohl bei der Klammersetzung durcheinander gekommen...
user profile icondubstep hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden C#-Quelltext
1:
                            textBox4.AppendText(Convert.ToString(cc.Value[j] + "; "));					
:?!?:
Also nochmal:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
           foreach (KeyValuePair<intint[]> cc in Adjazenzliste)
                {
                    textBox4.AppendText(Convert.ToString(cc.Key) + ": ");

                    foreach (int[] aa in Adjazenzliste.Values)
                    {
                        for (int j = 0; j < aa.Length-1; j++)
                        {
                            textBox4.AppendText(aa[j].ToString("000") + "; ");
                        }
                        textBox4.AppendText("\n");
                    }
                }
oder:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
           foreach (KeyValuePair<intint[]> cc in Adjazenzliste)
                {
                    textBox4.AppendText(Convert.ToString(cc.Key) + ": ");

                    foreach (int[] aa in Adjazenzliste.Values)
                    {
                        foreach(int value in aa)
                        {
                            textBox4.AppendText(value.ToString("000") + "; ");
                        }
                        textBox4.AppendText("\n");
                    }
                }