Hirdetés

Aktív témák

  • pmonitor

    aktív tag

    válasz cucka #71 üzenetére

    Adott a következő kód:
    MyHashSet<Point> myPt = new MyHashSet<Point>();
    myPt.Add(new Point(10, 20));
    myPt.Add(new Point(15, 20));
    myPt.Add(new Point(20, 20));
    myPt.Add(new Point(20, 20));

    Console.WriteLine(myPt.Count);


    int i;
    for (i = myPt.Count - 1; i >= 0; --i)
    {
    if (myPt[i].X == 20) myPt.Remove(myPt[i]);
    }
    Console.WriteLine(myPt.Count);

    i = myPt[myPt.Count - 1].X;

    Console.WriteLine(i);


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

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

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

    A kimenet:
    4
    2
    15

    Sztem tök jól működik. Az összes olyan myPt-t törölte, ahol X ==20. 2 darab ilyen volt. Látszik, hogy a hossz 4-ről 2-re változott.

Aktív témák