Autor Beitrag
FinalFantasy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Di 18.10.05 10:50 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Hashtable1.Add(1,4.5);
Hashtable1.Add(2,3.0);
Hashtable1.Add(4,0);
Hashtable1.Add(5,1);
Hashtable1.Add(6,0.5);
Hashtable1.Add(7,-2.0);
Hashtable1.Add(8,1.3);
Hashtable1.Add(3,7);

IDictionaryEnumerator en = Hashtable1.GetEnumerator();
en.Reset();

while(en.MoveNext()) 
{
  MachWas(en)
}


Warum kriege ich die Elemente mit dem Enumerator in umgedrehter Reihenfolge, wie ich sie hinzugefügt habe (trotz dem Reset am Anfang)?
Wie komm ich an die richtige Reihenfolge? Ist wichtig!!
Oder gibts Alternativen?


PS: Wie muss ich die C#-Tags schreiben? Die Controls gehen bei mir nicht.


Zuletzt bearbeitet von FinalFantasy am Di 18.10.05 10:59, insgesamt 1-mal bearbeitet
Martin1966
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1068

Win 2000, Win XP
Delphi 7, Delphi 2005
BeitragVerfasst: Di 18.10.05 10:56 
user profile iconFinalFantasy hat folgendes geschrieben:
PS: Wie muss ich die C#-Tags schreiben?

Steht in der Hilfe ;-) www.c-sharp-forum.de..._bbcodes_source.html

Lg Martin
Robert_G
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416


Delphi32 (D2005 PE); Chrome/C# (VS2003 E/A, VS2005)
BeitragVerfasst: Di 18.10.05 11:51 
Vielleicht wäre eine SortedList interessanter für dich?
Eine Hashtable ist ein Dictionary, d.h. du bekommst das zuerst, was du zuletzt reingeworfen hast. punkt.

Mit C# 2.0 könntest du einen Iterator[meta]siehe C# refence zu iterator oder yield[/meta] benutzen, der rückwärts duch die Keys/Values läuft.
Da es perfomancetechnisch ungünstig ist value types ständig in eine Box zu stecken und wieder raus zu friemeln: Das ganze natürlich als generic Dictionary. ;)

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:
class ReverseSortingDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
  public IEnumerable<KeyValuePair<TKey, TValue>> Reverse
  {
    get
    {
      if (Count > 0)
      {
        TKey[] keys = new TKey[Keys.Count];
        TValue[] values = new TValue[Values.Count];

        Keys.CopyTo(keys, 0);
        Values.CopyTo(values, 0);

        for (int i = Count - 1; i > 0; i--)
        {
          yield return new KeyValuePair<TKey, TValue>(keys[i], values[i]);
        }
      }
    }
  }
}

Jedes yield return entspricht einem Schleifendurchlauf, den du im Enumerator[meta]also auch im foreach[/meta] siehst.


Wenn man deine Werte nimmt, und sie nun richtig- und verkehrtrum darstellen lassen will, wäre das möglich:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
ReverseSortingDictionary<intdouble> dictionary = new ReverseSortingDictionary<intdouble>();
dictionary.Add(14.5);
dictionary.Add(23.0);
dictionary.Add(40);
dictionary.Add(51);
dictionary.Add(60.5);
dictionary.Add(7, -2.0);
dictionary.Add(81.3);
dictionary.Add(37);

Console.WriteLine("1)");
foreach (KeyValuePair<intdouble> entry in dictionary)
{
  Console.WriteLine("{0, 5} |{1, 5}", entry.Key, entry.Value);
}

Console.WriteLine("2)");
foreach (KeyValuePair<intdouble> entry in dictionary.Reverse)
{
  Console.WriteLine("{0, 5} |{1, 5}", entry.Key, entry.Value);
}
MagicAndre1981
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 18.10.05 11:52 
Wikipedia hat folgendes geschrieben:

Außerdem sind die Daten in einer Hashtabelle im Allgemeinem nicht sortiert, womit die sequentielle Ausgabe eines Teils der Einträge oder aller Einträge erheblich erschwert.


:wink:
FinalFantasy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Di 18.10.05 13:06 
Ähm, ja, mit den Dictonaries hab ich noch keine Erfahrung. Ich mache erst seit ca. ner Woche C#.
Ich werd mir das mal ansehen.

Was ich übrigens brauch, ist kein sortiertes Array, sondern ein First-In First-Out Array mit Indexen... Sortieren wäre sogar tötlich, weil ich die Werte eben in genau der Reihenfolge brauche.
Robert_G
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416


Delphi32 (D2005 PE); Chrome/C# (VS2003 E/A, VS2005)
BeitragVerfasst: Di 18.10.05 13:15 
user profile iconFinalFantasy hat folgendes geschrieben:
Ähm, ja, mit den Dictonaries hab ich noch keine Erfahrung. Ich mache erst seit ca. ner Woche C#.
Ich werd mir das mal ansehen.
Nicht vergessen, mein Code beispiel läuft _nicht_ mit C#1.1 und somit auch nicht mit .Net 1.1. Aber das dürfte wohl mit fast jedem meiner Bleistift Codes der Fall sein. ;)
FinalFantasy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Di 18.10.05 16:04 
Hmm, dann muss ich mir wohl doch selber etwas basteln...
Robert_G
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416


Delphi32 (D2005 PE); Chrome/C# (VS2003 E/A, VS2005)
BeitragVerfasst: Di 18.10.05 16:44 
user profile iconFinalFantasy hat folgendes geschrieben:
Hmm, dann muss ich mir wohl doch selber etwas basteln...
Ich kann dir nur eins empfehlen: f*** .Net 1.1 ;)
Gerade wenn du viel mit value types umzugehen hast sind generics und nullable types nicht nur performant besser, sie ersparen dir auch stumpfsinniges rumgecaste. ;)
Am 7. Nov. ist es final -> so what? :P