【问题标题】:foreach cannot operate on variables, cannot get enumerator, linkedlistsforeach 不能对变量进行操作,不能 getenumerator,链表
【发布时间】:2015-06-01 08:06:55
【问题描述】:

我有一个简单的程序,我们在我的编程课程中忙于单链表,我正在尝试计算不同类型对象的数量,我有一个类单链表,App,Onceoff:App,Distrubuted : 应用程序、单元格

我想要实现的是获取名为 Developed 的单链表中每个项目的对象类型,并尝试使用“is”语句确定它是哪种类型并增加所需的计数器,但 foreach 带有下划线跟随错误 “foreach 不能对 'PRr05.SinglyLinkedList' 类型的变量进行操作,因为 'PRr05.SinglyLinkedList' 不包含 'GetEnumarator' 的公共定义”,我不确定这意味着什么,但如果我是正确的,枚举器会返回类型,我该如何解决这个问题,下面的 SinglyLinkedList 类

(ps,是的,这是一个实用的,但我更进一步;))

FOREACH 方法

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;
            }
        }

单链表类

使用系统;

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;
            }
        }
    }

【问题讨论】:

  • 我假设Developed 是一个类,而不是Collection
  • 你能展示一下Developed的实现吗?

标签: c# foreach linked-list ienumerable singly-linked-list


【解决方案1】:

您需要对代码的语法和概念进行一些根本性的改变。例如,首先您应该知道foreach 仅与实现System.Collections.IEnumberableSystem.Collections.Generic.IEnumberable<T> 的对象一起使用。这里有一个转折;如果你想迭代你的Developed 类的(也许)成员,你应该使用反射;如果您想促进您的Developed 类与foreach 语句一起使用,您必须从我提到的前两个BCL 类之一派生。请考虑修改您的代码。

【讨论】:

  • 能否请您详细说明“...您应该使用反射;...”部分,反射是什么意思?
  • 通过反射,我的意思是您可以使用 System.Reflection 命名空间的类型建立的操作。通常,反射是一种从类型、模块、方法等中获取信息的可靠方法。例如,在您的代码中,如果您只想获取属性的标识符名称,您可以使用这种方式: foreach (System.Reflection.PropertyInfo item in typeof(Developed).GetProperties()) { ...your代码.....}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多