列表<元素>初始化引发“进程由于 StackOverflowException 而终止”

发布于 2024-08-26 14:45:37 字数 2003 浏览 7 评论 0原文

我有如下结构,当我进行初始化时:

ArrayList nodesMatrix = null;
List<vertex> vertexMatrix = null;
List<bool> odwiedzone = null;
List<element> priorityQueue = null;

vertexMatrix = new List<vertex>(nodesNr + 1);
nodesMatrix = new ArrayList(nodesNr + 1);
odwiedzone = new List<bool>(nodesNr + 1);
priorityQueue = new List<element>();

arr.NodesMatrix = nodesMatrix;
arr.VertexMatrix = vertexMatrix;
arr.Odwiedzone = odwiedzone;
arr.PriorityQueue = priorityQueue; //only here i have exception

调试器触发进程由于 StackOverflowException 而终止:/ 知道为什么这个集合会引发这个异常吗?

private struct arrays
    {
        ArrayList nodesMatrix;

        public ArrayList NodesMatrix
        {
            get { return nodesMatrix; }
            set { nodesMatrix = value; }
        }
        List<vertex> vertexMatrix;

        public List<vertex> VertexMatrix
        {
            get { return vertexMatrix; }
            set { vertexMatrix = value; }
        }
        List<bool> odwiedzone;

        public List<bool> Odwiedzone
        {
            get { return odwiedzone; }
            set { odwiedzone = value; }
        }
        public List<element> PriorityQueue
        {
            get { return PriorityQueue; }
            set { PriorityQueue = value; }
        }


    }
    public struct element : IComparable
    {
        public double priority
        {
            get { return priority; }
            set { priority = value; }
        }
        public int node
        {
            get { return node; }
            set { node = value; }
        }
        public element(double _prio, int _node)
        {
            priority = _prio;
            node = _node;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            element elem = (element)obj;
            return priority.CompareTo(elem.priority);
        }

        #endregion

I have structs like below and when I do that initialization:

ArrayList nodesMatrix = null;
List<vertex> vertexMatrix = null;
List<bool> odwiedzone = null;
List<element> priorityQueue = null;

vertexMatrix = new List<vertex>(nodesNr + 1);
nodesMatrix = new ArrayList(nodesNr + 1);
odwiedzone = new List<bool>(nodesNr + 1);
priorityQueue = new List<element>();

arr.NodesMatrix = nodesMatrix;
arr.VertexMatrix = vertexMatrix;
arr.Odwiedzone = odwiedzone;
arr.PriorityQueue = priorityQueue; //only here i have exception

debuger fires Process is terminated due to StackOverflowException :/
Some idea why this collection fires this exception ?

private struct arrays
    {
        ArrayList nodesMatrix;

        public ArrayList NodesMatrix
        {
            get { return nodesMatrix; }
            set { nodesMatrix = value; }
        }
        List<vertex> vertexMatrix;

        public List<vertex> VertexMatrix
        {
            get { return vertexMatrix; }
            set { vertexMatrix = value; }
        }
        List<bool> odwiedzone;

        public List<bool> Odwiedzone
        {
            get { return odwiedzone; }
            set { odwiedzone = value; }
        }
        public List<element> PriorityQueue
        {
            get { return PriorityQueue; }
            set { PriorityQueue = value; }
        }


    }
    public struct element : IComparable
    {
        public double priority
        {
            get { return priority; }
            set { priority = value; }
        }
        public int node
        {
            get { return node; }
            set { node = value; }
        }
        public element(double _prio, int _node)
        {
            priority = _prio;
            node = _node;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            element elem = (element)obj;
            return priority.CompareTo(elem.priority);
        }

        #endregion

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

著墨染雨君画夕 2024-09-02 14:45:37

您的 PriorityQueue 属性正在引用自身。
您需要更改访问器才能使用字段。

List<element> priorityQueue;
public List<element> PriorityQueue
{
    get { return priorityQueue; }
    set { priorityQueue = value; }
}

但是,您应该使用自动实现的属性:

public List<element> PriorityQueue { get; set; }

Your PriorityQueue property is referencing itself.
You need to change the accessors to use a field.

List<element> priorityQueue;
public List<element> PriorityQueue
{
    get { return priorityQueue; }
    set { priorityQueue = value; }
}

However, you should use automatically-implemented properties instead:

public List<element> PriorityQueue { get; set; }
乖不如嘢 2024-09-02 14:45:37

您的属性设置器是递归的。

    public List<element> PriorityQueue
    {
        get { return PriorityQueue; }
        set { PriorityQueue = value; }
    }

将其更改为:

    public List<element> PriorityQueue
    {
        get { return priorityQueue; }
        set { priorityQueue = value; }
    }

Your property setter is recursive.

    public List<element> PriorityQueue
    {
        get { return PriorityQueue; }
        set { PriorityQueue = value; }
    }

Change this to be:

    public List<element> PriorityQueue
    {
        get { return priorityQueue; }
        set { priorityQueue = value; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文