foreach无法对变量进行操作,无法获取枚举器,链表

赛百斯

我有一个简单的程序,我们在编程过程中忙于单链表,并且我尝试通过尝试计算不同类型的对象的数量进行实验,我有一个单链表类,即App,Onceoff:App,Distrubuted:App,细胞

我要实现的目标是获取名为Developed的单链接列表中每个项目的对象类型,并尝试使用“ is”语句确定其类型并递增所需的计数器,但是foreach带有以下错误并带有下划线“ foreach无法对类型为'PRr05.SinglyLinkedList'的变量进行操作,因为'PRr05.SinglyLinkedList'不包含'GetEnumarator'的公共定义。“我不确定这是什么意思,但是如果我正确,则Enumarator返回Type,我如何解决此问题,下面的SinglyLinkedList类

(ps,是的,这是实用的,但是我将进一步采取此步骤;))

预测方法

public int countApps(char Type)
        {
            int countO = 0, countD = 0, countR = 0;
            foreach (App item in Developed)
            {
                if (item is Onceoff)
                {
                    countO++;
                }
                else
                    if (item is Distributed)
                    {
                        countD++;
                    }
                    else countR++;
            }

            if (Type == 'O')
            {
                return countO;
            }

            if (Type == 'D')
            {
                return countD;
            }

            if (Type == 'R')
            {
                return countR;
            }
        }

SinglyLinkedList类

使用系统;

using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Pr05
{
    public class SinglyLinkedList       // NO CHANGES PERMITTED TO EXISTING METHODS IN THIS CLASS


     {
            // Sentinel cell
            private int counter;
            private Cell head;   // pointer to first cell in the list
            public SinglyLinkedList() 
            {
                counter = 0;
                head = null;
            }
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { get {return Current;}}
            object System.Collections.IEnumerator.Current { get { return Current; } }
            public int Count()  
            {
                return counter;
            }
            public Cell getFirst() 
            {
                return head;
            }
            public void addFirst(Object newItem) // Section Adding Cells at the Beginning pp 59 - 60
            /* pre:  Have object to be added to calling singly linked list object, which may be empty.
             * post: newItem is the element of the FIRST cell of the singly linked list.  All other existing cells of the
             *       singly linked list retain their ordering AFTER the new first cell.
             *       The counter is modified to reflect the addition of a new cell to the singly linked list. */
            {
                // ADD CODE FOR addFirst HERE
                Cell newCell = new Cell(newItem);
                newCell.setNext(head);
                head = newCell;
                counter++;
            }
            public void addLast(Object newItem) // Section Adding Cells at the End pp 60 - 61
            /* pre:  Have object to be added to calling singly linked list object, which may be empty.
             * post: newItem is the element of the LAST cell of the singly linked list.  All other existing cells of the
             *       singly linked list retain their ordering BEFORE the new last cell.
             *       The counter is modified to reflect the addition of a new cell to the singly linked list. 
             * CAREFUL: C# has certain restrictions which do not allow direct implemention of the code as specified in the 
             *          prescribed text.  Find a way around the restriction. */
            {
                // ADD CODE FOR addLast HERE
                if (this.Count() == 0)
                {
                    this.addFirst(newItem);
                    return;
                }
                Cell newCell = new Cell(newItem);
                Cell cur = head;
                while (cur.next() != null)
                    cur = cur.next();
                cur.setNext(newCell);
                counter++;
            }
            public Cell removeFirst()
            /* pre:  Have at least one cell in calling singly linked list object.
             * post: Return the cell removed, which is the first cell in the list.
             *       The counter is modified to reflect the removal of the first cell from the singly linked list. */
            {
                // ADD CODE FOR removeFirst HERE
                Cell cur = head;
                head = cur.next();
                cur.setNext(null);
                counter--;
                return cur;

            }
            public Cell removeLast()
            /* pre:  Have at least one cell in calling singly linked list object.
             * post: Return the cell removed, which is the last cell in the list.
             *       The counter is modified to reflect the removal of the last cell from the singly linked list. 
             * CAREFUL: C# has certain restrictions - find a way around the restriction. */
            {
                // ADD CODE FOR removeLast HERE
                Cell cur;
                if (this.Count() == 1)
                {
                    cur = head;
                    this.Clear();
                    return cur;
                }
                cur = head;
                Cell prev = head;
                while (cur.next() != null)
                {
                    prev = cur;
                    cur = cur.next();
                }
                prev.setNext(null);
                counter--;
                return cur;
             }
            public void addBefore(Object newItem, Cell link)
            /* pre:  Have object to be added to calling singly linked list object, and a link in the singly linked list BEFORE
             *       which the newItem's cell must be added.
             * post: newItem is the element of the added cell of the singly linked list.  All other existing cells of the
             *       singly linked list retain their ordering relevant to the position of the newly added cell.
             *       The counter is modified to reflect the addition of a new cell to the singly linked list. */
            {
                // ADD CODE FOR addBefore HERE
                if (link == null)  // list either empty or must be added at end of list
                {
                    this.addLast(newItem);
                    return;
                }
                Cell newCell = new Cell(newItem);
                Cell cur = head;
                if (cur == link)  // must be added as first cell
                {
                    this.addFirst(newItem);
                    return;
                }
                while (cur.next() != link)
                    cur = cur.next();
                cur.setNext(newCell);
                newCell.setNext(link);
                counter++;
            }
            public void addAfter(Object newItem, Cell link) // Section Inserting Cells After Other Cells pp 61 - 62
            /* pre:  Have object to be added to calling singly linked list object, and a link in the singly linked list AFTER
             *       which the newItem's cell must be added.
             * post: newItem is the element of the added cell of the singly linked list.  All other existing cells of the
             *       singly linked list retain their ordering relevant to the position of the newly added cell.
             *       The counter is modified to reflect the addition of a new cell to the singly linked list. */
            {
                // ADD CODE FOR addAfter HERE
                if (link == null)
                {
                    this.addLast(newItem);
                    return;
                }
                Cell newCell = new Cell(newItem);
                newCell.setNext(link.next());
                link.setNext(newCell);
                counter++;
            }
            public void Clear()
            /* pre:  Have calling singly linked list object, which could be empty.
             * post: EFFICIENTLY clear the singly linked list. */
            {
                // ADD CODE FOR Clear HERE
                counter = 0;
                head = null;
            }
            // OPTIONAL TASKS
            public Cell removeBefore(Cell link)
            /* pre:  Have at least one cell in calling singly linked list object. Have a link in the singly linked list.
             * post: Return the cell removed, which is the  cell BEFORE the given link.
             *       The counter is modified to reflect the removal of the cell from the singly linked list. */
            {
                // ADD CODE FOR removeBefore HERE
                if (link == head) // nothing to remove in front of link
                    return null;
                if (link == null) // then remove last cell
                    return removeLast();
                Cell cur = head;
                Cell prev = head;
                while (cur.next() != link)
                {
                    prev = cur;
                    cur = cur.next();
                }
                if (cur == head)
                    return removeFirst();
                prev.setNext(link);
                cur.setNext(null);
                counter--;
                return cur;
            }
            public Cell removeAfter(Cell link) // Section Deleting Cells pp 62 - 63
            /* pre:  Have at least one cell in calling singly linked list object. Have a link in the singly linked list.
             * post: Return the cell removed, which is the  cell AFTER the given link.
             *       The counter is modified to reflect the removal of the cell from the singly linked list. */
            {
                // ADD CODE FOR removeAfter HERE
                if (link == null)   // nothing after
                    return null;
                if (link.next() == null) // then nothing after link
                    return null;
                Cell cur = link.next();
                link.setNext(cur.next());
                cur.setNext(null);
                counter--;
                return cur;
            }
        }
    }
无能为力

您需要在语法和代码概念上进行一些必要的根本性更改。例如,首先您应该知道foreach只能与实现System.Collections.IEnumberable的对象一起使用System.Collections.Generic.IEnumberable<T>这里有一个转折。如果要遍历Developed类的(可能是)成员,则应使用反射;如果您想使您的Developed班级可以与foreach语句一起使用,则必须从BCL我提到的两个第一班中的一个派生请考虑修改您的代码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

lldb:无法实现:无法获取变量的值

Foreach ADO枚举器不显示ADO对象源变量

由于“”不包含“ GetEnumerator”的公共定义,因此foreach语句获取错误无法对类型为“”的变量进行操作

foreach控制器无法正确使用变量

LINQ TO ENTITY无法与枚举类型进行比较

如何在SSIS中使用“从变量枚举器进行Foreach”以编程方式创建foreach循环

我无法在服务器上使用phpmyadmin进行操作

无法创建枚举变量

无法获取Pyglet进行渲染

无法对特定事物进行操作

在使用SwiftUI的ForEach循环中无法获取@state变量

无法从控制器获取异常以进行查看

错误:foreach语句无法对'System.Web.UI.WebControls.ListItem'类型的变量进行操作

无法获取枚举

foreach语句无法对类型为'System.Data.DataSet'的变量进行操作

我无法使操作正常进行

因为Foreach语句不包含GetEnumerator的公共定义,所以Foreach语句无法对类型的变量进行操作

无法获取新的Ubuntu服务器进行更新/升级

错误:“ foreach语句无法对变量进行操作”

无法访问控制器中的获取变量

无法进行 SUM 操作 SQL

foreach 错误:无法对类型进行操作,因为它不包含“GetEnumerator”的公共定义

foreach 语句无法对“Customers”类型的变量进行操作,因为“GetEnumerator”没有公共实例定义

无法对链表进行排序

C# foreach 修改列表。收藏已被修改;可能无法执行枚举操作

无法对链表进行排序

获取错误 foreach 语句无法对“xyz”类型的变量进行操作,因为“xyz”不包含“GetEnumerator”的公共定义

无法让变量在 for 中执行操作

错误 CS1579 foreach 语句无法对“GameObject”类型的变量进行操作