亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用方法RemoveRange時List中的ArgumentOutOfRange

使用方法RemoveRange時List中的ArgumentOutOfRange

C#
料青山看我應如是 2021-05-04 13:39:17
所以我有一個包含379個元素的列表,我想刪除其中的最后8個元素我正在使用List<Point> points= new List<Point>(); ... points.RemoveRange(points.Count-8,8);但它拋出ArgumentOutOfRangeException:需要非負數。參數名稱:索引所以我有清單的課看起來像這樣上課要點: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);        }    }}和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();}我已經刪除了PointsMap中的一些循環,但是您已經知道發生了什么。
查看完整描述

1 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

您的繪制函數假設removerange索引和offset是8的倍數。

情況似乎并非如此:points.Count%8!=0。

您可以使用下面的代碼來處理其余的代碼。


    public void Draw(RenderWindow window)

    {

        var n = 8;

        var index = Math.Max(0, points.Count - n);

        var offset = Math.Min(points.Count, n);

        points.RemoveRange(index, offset);


        foreach (Point point in points)

        {

           point.Draw(window);

        }

     }


查看完整回答
反對 回復 2021-05-29
  • 1 回答
  • 0 關注
  • 193 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號