Hirdetés

Keresés

Aktív témák

  • pmonitor

    aktív tag

    válasz pmonitor #72 üzenetére

    Átírtam a kódot. Én nem találok olyan adatokat, amivel ne működne.
    namespace TesztCsharp
    {
    class HashSets
    {
    static void Main(string[] args)
    {
    MyHashSet<Point> myPt = new MyHashSet<Point>();
    myPt.Add(new Point(20, 20));
    myPt.Add(new Point(15, 20));
    myPt.Add(new Point(20, 20));
    myPt.Add(new Point(21, 20));

    Console.WriteLine("Count = {0}", myPt.Count);
    foreach (Point pt in myPt)
    {
    Console.Write("{0} ", pt.X);
    }

    int i;
    int torl = 20;
    Console.WriteLine("\nTorlendo X érték: {0}", torl);
    for (i = myPt.Count - 1; i >= 0; --i)
    {
    Point pt = myPt[i];
    if (pt.X == torl) myPt.Remove(pt);
    }

    Console.WriteLine("\n\nCount = {0}", myPt.Count);
    foreach (Point pt in myPt)
    {
    Console.Write("{0} ", pt.X);
    }

    if (myPt.Count > 0)
    {
    i = myPt[myPt.Count - 1].X;
    Console.WriteLine("\nUtolso elem = {0}", i);
    }
    }
    }

    public class MyHashSet<T> : HashSet<T>
    {
    List<T> list = new List<T>();
    public T this[int index]
    {
    get
    {
    if (list.Count != Count) list = new List<T>(this);
    return ((list.Count > index) && (index >= 0)) ? list[index] : throw new IndexOutOfRangeException();
    }
    }

    public bool Add(T item)
    {
    if (base.Add(item))
    {
    list = new List<T>(this);
    return true;
    }
    else return false;
    }

    public bool Remove(T item)
    {
    if (base.Remove(item))
    {
    list = new List<T>(this);
    return true;
    }
    else return false;
    }
    }

    public class Point
    {
    public int X; public int Y;

    public Point(int x, int y)
    {
    X = x;
    Y = y;
    }
    }
    }

    A kimenet pedig ezekkel az adatokkal:
    Count = 4
    20 15 20 21
    Torlendo X érték: 20


    Count = 2
    15 21
    Utolso elem = 21

    cucka: Azért köszi a kritikát. A véleményünk nem egyezik, de örülök, hogy leírtad az álláspontod...

Aktív témák