计算ListNodes的总和 [英] calculate the sum of ListNodes
本文介绍了计算ListNodes的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算ListNodes的总和,但是它说''指定演员表是无效的。
使用系统;
使用 System.Collections;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
命名空间 ConsoleApplication1
{
class 计划
{
静态 void Main( string [] args)
{
List listOfNodes = new List();
随机r = 新 Random();
int sum = 0 ;
for ( int i = 0 ; i < 10 ; i ++)
{
listOfNodes.addObjects (r.Next( 1 , 100 ));
}
listOfNodes.DisplayList();
foreach ( object s in listOfNodes)
sum + =( int )s;
Console.WriteLine(sum);
Console.ReadLine();
}
class ListNode
{
public object inData { get ; private set ; }
public ListNode Next { get ; set ; }
public ListNode( object dataValues)
: this (dataValues, null ){}
public ListNode( object dataValues,
ListNode nextNode)
{
inData = dataValues; Next = nextNode;
}
} // class ListNode -s dasasruli
public class 列表:IEnumerable
{
private ListNode firstNode,lastNode;
private string name;
public 列表( string nameOfList)
{
name = nameOfList;
firstNode = lastNode = null ;
}
public List()
: this ( listOfNodes){}
public void addObjects( object inItem)
{
if (isEmpty())
{firstNode = lastNode = new ListNode(inItem); }
else {firstNode = new ListNode(inItem,firstNode); }
}
private bool isEmpty()
{
return firstNode == null ;
}
public void DisplayList()
{
if (isEmpty())
{Console.Write( < span class =code-string> Empty + name); }
else
{
Console.Write( + name + 是:\ n跨度>);
ListNode current = firstNode;
while (current!= null )
{
Console.Write(current.inData + );
current = current.Next;
}
Console.WriteLine( \ n);
}
}
public IEnumerator GetEnumerator()
{
ListNode node = firstNode ;
while (node!= null )
{
yield return node;
node = node.Next;
}
}
} // 班级列表
}
}
解决方案
你不能将ListNode强制转换为整数 - 这太傻了! />
foreach ( object s listOfNodes)
sum + =( int )s;
s是一个ListNode - 因为这是你的GetEnumerator方法返回的:
public IEnumerator GetEnumerator()
{
ListNode node = firstNode;
while (node!= null )
{
yield return node;
node = node.Next;
}
}
可能你真正想做的是改变GetEnumerator来代替返回值:
public IEnumerator GetEnumerator()
{
ListNode node = firstNode;
while (node!= null )
{
yield return node.inData;
node = node.Next;
}
}
OriginalGriff建议更改枚举器返回的内容。
我'反对这一点,因为它不是GetEnumerator()
的预期语义。
我使用object
更少且更正的类型更多:
public class 列表:IEnumerable< ListNode> ...
和
public 的IEnumerator< ListNode> GetEnumerator()...
然后总和可以是:
foreach (ListNode s in listOfNodes)
sum + =( int )s.inData;
或者,使用Linq:
sum = listOfNodes.Sum(s = > ( int )s.inData);
当然,如果ListNode.inData
将总是是int
,
然后声明它,并且转换为int
是不必要的。
I want to calculate the sum of ListNodes, but it says that "'s' Specified cast is not valid."
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List listOfNodes = new List();
Random r = new Random();
int sum = 0;
for (int i = 0; i < 10; i++)
{
listOfNodes.addObjects(r.Next(1, 100));
}
listOfNodes.DisplayList();
foreach (object s in listOfNodes)
sum += (int)s;
Console.WriteLine(sum);
Console.ReadLine();
}
class ListNode
{
public object inData { get; private set; }
public ListNode Next { get; set; }
public ListNode(object dataValues)
: this(dataValues, null) { }
public ListNode(object dataValues,
ListNode nextNode)
{
inData = dataValues; Next = nextNode;
}
} // class ListNode-s dasasruli
public class List : IEnumerable
{
private ListNode firstNode, lastNode;
private string name;
public List(string nameOfList)
{
name = nameOfList;
firstNode = lastNode = null;
}
public List()
: this("listOfNodes") { }
public void addObjects(object inItem)
{
if (isEmpty())
{ firstNode = lastNode = new ListNode(inItem); }
else { firstNode = new ListNode(inItem, firstNode); }
}
private bool isEmpty()
{
return firstNode == null;
}
public void DisplayList()
{
if (isEmpty())
{ Console.Write("Empty " + name); }
else
{
Console.Write("The " + name + " is:\n");
ListNode current = firstNode;
while (current != null)
{
Console.Write(current.inData + " ");
current = current.Next;
}
Console.WriteLine("\n");
}
}
public IEnumerator GetEnumerator()
{
ListNode node = firstNode;
while (node != null)
{
yield return node;
node = node.Next;
}
}
}//end of class List
}
}
解决方案
You can't cast a ListNode to an integer - that's just silly!
foreach (object s in listOfNodes) sum += (int)s;s is a ListNode - because that is what your GetEnumerator method returns:
public IEnumerator GetEnumerator() { ListNode node = firstNode; while (node != null) { yield return node; node = node.Next; } }
Probably, what you actually want to do is change the GetEnumerator to return the value instead:
public IEnumerator GetEnumerator() { ListNode node = firstNode; while (node != null) { yield return node.inData; node = node.Next; } }
OriginalGriff suggested changing what the enumerator returned.
I'd argue against that because it is not the expected semantics ofGetEnumerator()
.
I'd useobject
less and correct types more:
public class List : IEnumerable<ListNode> ...
and
public IEnumerator<ListNode> GetEnumerator() ...
then the summation can be:
foreach (ListNode s in listOfNodes) sum += (int)s.inData;
or, using Linq:
sum = listOfNodes.Sum(s => (int)s.inData);
And, of course, ifListNode.inData
will always beint
,
then declare it as such and the cast toint
is unnecessary.
这篇关于计算ListNodes的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文