使用方法RemoveRange时,List中的ArgumentOutOfRange

时间:2018-05-05 01:10:53

标签: c# .net linq

所以我有一个包含379个元素的列表,我想删除其中的8个

我正在使用

List<Point> points= new List<Point>();
...
points.RemoveRange(points.Count-8,8);

但它会抛出ArgumentOutOfRangeException

  

需要非负数。参数名称:index

所以我有列表的类看起来像这样

namespace XanMan.NET
{
    class PointsMap
    {
        List<Point> points = new List<Point>();

        public PointsMap()
        {
            float x = 80;
            float y = 655;
            for (int i=0;i<28;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 680;
            for (int i=0;i<26;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 80;
            y = 55;
            for (int i=0;i<56;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 50;
            y = 455;
            for (int i=0;i<14;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 405;
            for (int i=0;i<22;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 880;
            for (int i=0;i<18;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            y = 215;
            x = 55;
            for (int i=0;i<15;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 400;
            for (int i=0;i<16;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 880;
            for (int i=0;i<18;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 760;
            y = 335;
            for (int i=0;i<4;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 880;
            for (int i=0;i<10;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            x = 55;
            y = 80;
            for (int i=0;i<6;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            x = 375;
            y = 80;
            for (int i=0;i<6;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            y = 80;
            x = 535;
            for (int i=0;i<6;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            y = 80;
            x = 735;
            for (int i=0;i<6;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            x = 895;
            y = 80;
            for (int i=0;i<6;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            x = 1215;
            y = 80;
            for (int i=0;i<6;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            y = 240;
            x = 175;
            for (int i=0;i<10;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            y = 240;
            x = 375;
            for (int i=0;i<10;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            y = 240;
            x = 535;
            for (int i=0;i<10;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            y = 240;
            x = 735;
            for (int i=0;i<10;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            x = 855;
            y = 240;
            for (int i=0;i<10;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            y = 240;
            x = 1095;
            for (int i=0;i<10;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            for (int i=0;i<8;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            x = 855;
            y = 480;
            for (int i=0;i<8;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            x = 1215;
            y = 480;
            for (int i=0;i<8;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                y += 20;
            }
            y = 215;
            x = 720;
            for (int i=0;i<6;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }
            y = 455;
            x = 330;
            for (int i=0;i<2;i++)
            {
                points.Add(new Point(new Vector2f(x, y)));
                x += 20;
            }

            points.Add(new Point(new Vector2f(50, 50)));
            points.Add(new Point(new Vector2f(1210, 50)));
            points.Add(new Point(new Vector2f(50, 650)));
            points.Add(new Point(new Vector2f(1210, 650)));
            points.Add(new Point(new Vector2f(370, 450)));
            points.Add(new Point(new Vector2f(370, 210)));
            points.Add(new Point(new Vector2f(850, 210)));
            points.Add(new Point(new Vector2f(850, 450)));
        }

        public void Draw(RenderWindow window)
        {
            points.RemoveRange(points.Count - 8, 8);
            foreach (Point point in points)
            {
                point.Draw(window);
            }
        }
    }
}

class Point:

namespace XanMan.NET
{
    class Point
    {
        protected Texture texture;
        protected RectangleShape body;
        protected Vector2f position;
        public const uint point = 10;

        public Point(Vector2f position)
        {
            texture = new Texture("point.png");
            body = new RectangleShape(new Vector2f(10, 10))
            {
                Texture = texture,
                Position = position
        };

        }
        protected Point()
        {
        }

        public void Draw(RenderWindow window)
        {
            window.Draw(body);
        }
    }
}

带有main

的program.cs
class Program
{
    static RenderWindow window;
    static GAMESTATE gamestate;
    static Map map;
    static Menu menu;

    static void Main(string[] args)
    {
        window = new RenderWindow(new VideoMode(1280, 780), "XanMan.NET");
        gamestate = GAMESTATE.mainmenu;
         menu = new Menu(gamestate);
        map = new Map();
        PointsMap mapOfPoints = new PointsMap();
        window.Closed += Window_Closed;
        window.KeyReleased += menu.Update;


        while (window.IsOpen)
        {
            window.DispatchEvents();
            window.Clear();
            map.Draw(window);
            mapOfPoints.Draw(window);
            window.Display();
        }
    }

    private static void Window_Closed(object sender, EventArgs e) => window.Close();
}

由于stackoverflow,我删除了PointsMap中的一些循环,但你已经知道发生了什么。

1 个答案:

答案 0 :(得分:0)

您的绘图函数假设,删除范围索引和偏移量是8的倍数 情况似乎并非如此:points.Count%8!= 0.
您可以使用以下代码,只需处理剩下的剩余时间。

function flatten(arr) {
  if (Array.isArray(arr)) {
    return arr.reduce((prev, next) => {
      return prev.concat(flatten(next));
    }, []);
  } else {
    return arr;
  }
}

console.log(flatten([1, [2, 3], [4, [5, 6]]])); // 1, 2, 3, 4, 5, 6