最近在努力学习C#语法...今晚在左一个二叉树的迭代遍历时发生了点错误...
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumeratorTestForPair
{
class BinaryTreeT> : IEnumerableT> where T : IComparable
{
public BinaryTree(T value)
{
Value = value;
}
private T _Value;
public T Value
{
get { return _Value; }
set { _Value = value; }
}
private PairBinaryTreeT>> _SubItems;
public PairBinaryTreeT>> SubItems
{
get
{
return _SubItems;
}
set
{
IComparable first;
first = value.First._Value;
if (first.CompareTo(value.Second._Value) 0)
{
}
else
{
}
_SubItems = value;
}
}
//[System.Runtime.CompilerServices.IndexerName("Entry")]
public T this[PairItem[] branches]
{
get
{
BinaryTreeT> currentNode = this;
int totalLevel = (branches == null) ? 0 : branches.Length;
int currentLevel = 0;
while (currentLevel totalLevel)
{
currentNode = currentNode.SubItems[branches[currentLevel]];
if (currentNode == null)
{
throw new IndexOutOfRangeException();
}
++currentLevel;
}
return currentNode.Value;
}
}
#region IEnumerable Members
public IEnumeratorT> GetEnumerator()
{
yield return Value;
foreach (BinaryTreeT> tree in SubItems)
{
if (tree != null)
{
foreach (T item in tree)
{
yield return item;
}
}
}
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumeratorTestForPair
{
class PairT> : IPairT>,
IEnumerableT>
{
public Pair(T first)
{
this._First = first;
this._Second = default(T);
}
public Pair(T first, T second)
{
this._First = first;
this._Second = second;
}
#region IPair Members
public T First
{
get
{
return _First;
}
private set
{
_First = value;
}
}
private T _First;
public T Second
{
get
{
return _Second;
}
private set
{
this._Second = value;
}
}
private T _Second;
public T this[PairItem index]
{
get
{
switch (index)
{
case PairItem.First:
return First;
case PairItem.Second:
return Second;
default:
throw new NotImplementedException(
string.Format("The enum {0} has not been implemented."));
}
}
set
{
switch (index)
{
case PairItem.First:
First = value;
break;
case PairItem.Second:
Second = value;
break;
default:
throw new NotImplementedException(
string.Format("The enum {0} has not been implemented."));
}
}
}
#endregion
#region IEnumerable Members
public IEnumeratorT> GetEnumerator()
{
yield return First;
yield return Second;
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
public System.Collections.Generic.IEnumerableT>
GetNotNullEnumerator()
{
if (First == null || Second == null)
yield break;
yield return Second;
yield return First;
}
public System.Collections.Generic.IEnumerableT>
GetReverseEnumerator()
{
yield return Second;
yield return First;
}
}
}
而调试代码是:...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumeratorTestForPair
{
class Program
{
static void Main(string[] args)
{
BinaryTreestring> jfkFamilyTree = new BinaryTreestring>("John Fizgerald Kennedy");
jfkFamilyTree.SubItems = new PairBinaryTreestring>>(
new BinaryTreestring>("Joseph Patrick Kennedy"),
new BinaryTreestring>("Rose Elizabeth Fizgrald"));
jfkFamilyTree.SubItems.First.SubItems = new PairBinaryTreestring>>(
new BinaryTreestring>("Patrick Joseph Kennedy"),
new BinaryTreestring>("Mary Augusta Hickey"));
jfkFamilyTree.SubItems.Second.SubItems = new PairBinaryTreestring>>(
new BinaryTreestring>("John Francis Fizgerald"),
new BinaryTreestring>("Mary Hoseph Hannom"));
foreach (string name in jfkFamilyTree)
{
Console.WriteLine(name);
}
}
}
}
结果发生问题说 迭代到树尖时SubItems为null....后来在迭代中加了个判断是否为null就貌似解决了....但不对啊???为什么书没有啊?难道输错了...
最后 终于找到问题是录入错误把Pair弄成了class...而BinaryTree中持有一个Pair类型的SubItems的“引用”...不是值类型..这样会直接初始化为null所以错了
那怎么让他初始化为一个默认的Pair 加个构造函数可以 但书上是把class改成Struct后就是持有一个值类型了...这样初始化会调用一个结构的默认值(感觉像默认构造函数...),就是一个Pair...每个成员都是null...终于搞懂了...
哈哈 说了这么多 我今晚被这个也搞了好长时间。。。所以在这说下,其实就是值类型和引用的区别但是用的时候就忘了..留在这提醒下自己,也希望对大家有用...第一篇博客哈!
分享到:
相关推荐
Visual Studio是微软提供的一个强大的集成开发环境(IDE),支持C#编程。以下是对标题和描述中涉及的知识点的详细说明: 1. **线性表**:线性表是最基本的数据结构,包含顺序排列的元素集合。在C#中,可以使用...
在C#编程中,生成类文件的文档是提高代码可读性和维护性的重要步骤。类文档不仅方便开发者理解和使用API,还可以减少代码注释的工作量。本话题将深入探讨如何自动生成C#类中的属性和方法的文档,以及涉及到的相关...
在这个例子中,C# 的 `switch` 语句有一个重要的特点:如果没有 `break` 语句,程序会继续执行下一个 `case`。因此,当 `Grade / 10` 等于 7 时,`case 7:` 和 `case 6:` 都会被执行,导致输出 "你的成绩为:C" 和 ...
这是一个用于生成和处理PDF文档的C#库,它提供了全面的功能来创建、编辑、阅读和保护PDF文件。通过Spire.Pdf,开发者可以轻松地添加文本、图像、形状、超链接,也可以插入和编辑表格,设置页面布局,调整页面大小和...
结合这两个文件,开发者可以在C#环境中通过`SqliteHelper.cs`进行数据库操作,并使用`SQLiteExpertPersSetup.exe`进行数据库设计和管理,形成一个完整的Sqlite开发和维护流程。这样的工具集合对于简化C#开发中的...
在服务器端,C#可以创建一个Web服务(如ASMX或WCF服务),暴露可被JavaScript调用的方法。通过JSON或XML格式进行数据交换,实现客户端和服务器端的通信。在.NET中,`System.Web.Script.Services.ScriptService`特性...
标题提到的"MYSQL 生成C#实体类"是一个工具或者源码,它能帮助开发者自动生成对应于MySQL数据库表结构的C#实体类代码,但需要注意的是,这个工具可能不包含文件生成功能,仅提供代码文本输出。 首先,让我们理解...
通过这个"C#数据结构实践项目源程序",你可以学习到如何在C#环境中设计、实现和优化各种数据结构,同时提升解决问题的能力。这个项目将帮助你更好地理解数据结构的内部工作原理,以及如何在实际项目中选择和使用合适...
总的来说,开发“带树型结构的下拉框控件”是一个挑战性的任务,但通过理解和掌握C# WinForms编程,我们可以构建出强大的自定义控件,提升用户界面的可用性和效率。对于那些处理复杂数据结构的WINFORM应用程序来说,...
3. **栈和队列**:栈是一种后进先出(LIFO)的数据结构,C#中的System.Collections.Stack类可以用来实现栈。队列是先进先出(FIFO)的数据结构,C#中的System.Collections.Queue类可用来实现队列。 4. **串**:串是...
2. 链表:链表是由节点构成的数据结构,每个节点包含数据和指向下一个节点的引用。C#中可以使用LinkedList类来实现链表。 3. 栈:栈是一种后进先出(LIFO)的数据结构,常用于函数调用、表达式求值等场景。C#中的...
2. **链表**:由一系列节点组成,每个节点包含数据和指向下一个节点的引用。C#中的LinkedList类实现了链表。 3. **栈**:遵循“后进先出”(LIFO)原则,C#中的System.Collections.Generic.Stack类提供了栈的操作。...
4. C#中的数据结构实现:本书详细讨论了各种数据结构在C#中的实现方法,包括各种基本数据结构的接口、节点类和数据结构类的C#代码,以及常用算法。 5. .NET框架中数据结构的实现:在.NET框架中,存在许多内置的数据...
9. 哈希表:哈希表是一种通过哈希函数实现快速查找的数据结构,C#中的Dictionary, TValue>和HashSet类就是哈希表的应用。 10. 链接与递归:理解指针和引用的概念,学习如何在C#中使用引用类型进行链式操作。同时,...
描述中的 "一个C#实现的数据结构动画演示系统.zip" 强调了这是一个可下载的压缩包文件,包含了整个系统的源代码和可能的资源文件。用户解压后,可以查看源代码学习C#编程和数据结构的实现,或者运行程序进行学习和...
c数据结构教程C#结构教程.pdf
11. **泛型接口与抽象类**:C#中的泛型接口(如IEnumerable)和抽象类(如List的基类IList)为实现各种数据结构提供了统一的访问模式。 12. **LINQ**:Language Integrated Query(语言集成查询)是C#的一个强大...
1. **初始化Word应用实例**:使用`Application`类创建一个Word应用程序的实例,这样就可以在后台运行Word,而不打开用户界面。 ```csharp Microsoft.Office.Interop.Word.Application wordApp = new Microsoft....
在C#编程语言中,理解和掌握数据结构对于开发高效、优化的软件系统至关重要。本资源"数据结构(C#语言版).rar"包含了一份PDF文档,很可能是对数据结构的详细讲解,以C#语言为实现背景。 在C#中,我们经常会遇到以下...
4. 栈与队列:栈是一种后进先出(LIFO)的数据结构,C#中的System.Collections.Generic.Stack可以方便地创建和操作栈。队列是一种先进先出(FIFO)的数据结构,C#提供了Queue类来实现。 5. 树形结构:二叉树是最...