最近在努力学习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...终于搞懂了...
哈哈 说了这么多 我今晚被这个也搞了好长时间。。。所以在这说下,其实就是值类型和引用的区别但是用的时候就忘了..留在这提醒下自己,也希望对大家有用...第一篇博客哈!
分享到:
相关推荐
将数据结构与C#语言和.NET框架结合是本书的一大特点。本书分为8章,第1章介绍了数据结构和算法的基本概念及本书用到的数学和C#的知识;第2章至第6章分别讨论了线性表、栈和队列、串和数组、树型结构和图结构等常用的...
5.2.4二叉链表存储结构的类实现....................................132 5.2.5 二叉树的遍历...................137 5.3 树与森林.......................................141 5.3.2 树、森林与二叉树的转换..........
将数据结构与C#语言和.NET框架结合是本书的一大特点。本书分为8章,第1章介绍了数据结构和算法的基本概念及本书用到的数学和C#的知识;第2章至第6章分别讨论了线性表、栈和队列、串和数组、树型结构和图结构等常用的...
结构:不能从另外一个结构或者类继承,本身也不能被继承,虽然结构没有明确的用sealed声明,可是结构是隐式的sealed . 类:完全可扩展的,除非显示的声明sealed 否则类可以继承其他类和接口,自身也能被继承 注:...
2.5 C#中的线性表.................................................................................................................64 本章小结...........................................................
.NET体系结构.... 3 1.1 C#与.NET的关系...... 3 1.2 公共语言运行库...... 3 1.3 详细介绍中间语言...... 6 1.3.1 面向对象和接口的支持..... 7 1.3.2 相异值类型和引用类型..... 8 1.3.3 强数据类型..... 8 1.3.4 ...
将数据结构与C#语言和.NET框架结合是本书的一大特点。本书分为8章,第1章介绍了数据结构和算法的基本概念及本书用到的数学和C#的知识;第2章至第6章分别讨论了线性表、栈和队列、串和数组、树型结构和图结构等常用的...
C#.Net中类与结构的区别,帮你更好掌握C#基础,我觉的非常不错
C#数组、结构和枚举PPT.ppt
c#中结构数组的定义,很全的
1.1.2 基本概念和术语...............................................................................................1 1.2 算法..............................................................................
数据结构的基本分类 两大类: (一)线性结构(线性表) 数据元素之间的逻辑关系可以用一个线性序列简单地表示出来。 线性表是典型的线性结构,它的数据元素只按先后次序联接。 表、栈、队列、字串、数组和文件等方式...
C# 中基于 COM+ 的结构化存储,C#代码与javaScript函数的相互调用,C#共享内存操作类
类和结构 类和结构 类和结构 类和结构 类和结构 类和结构
将C# 类文件中属性和方法自动生成文档,C#类文档结构化生成
第2章 C#程序基本结构.ppt
第一章 程序基础 1.1 什么是.Net Framework.....................................................1 1.2 什么是 C#语言............................................................1 1.3 托管代码与非托管代码.....
2.2 第一个C#程序...... 23 2.2.1 代码..... 24 2.2.2 编译并运行程序..... 24 2.2.3 详细介绍..... 25 2.3 变量........ 27 2.3.1 变量的初始化..... 27 2.3.2 变量的作用域..... 28 2.3.3 常量..... 31 2.4 预定义...