gpt4 book ai didi

使用列表的 C# 递归编程

转载 作者:行者123 更新时间:2023-12-02 05:31:57 25 4
gpt4 key购买 nike

我正在开发一个程序,其中每个项目都可以包含一个项目数组(我正在制作一个菜单,它具有树状结构)

目前我将这些项目作为列表而不是数组,但我觉得我没有充分利用它来简化代码。我选择了列表而不是标准数组,因为接口(interface)(.add、.remove 等)很有意义。

我有代码来搜索结构并返回名称的路径(即 Item.subitem.subsubitem.subsubsubitem)。下面是我的代码:

public class Item
{
//public Item[] subitem; <-- Array of Items
public List<Item> subitem; // <-- List of Items

public Color itemColor = Color.FromArgb(50,50,200);
public Rectangle itemSize = new Rectangle(0,0,64,64);
public Bitmap itemBitmap = null;
public string itemName;


public string LocateItem(string searchName)
{
string tItemName = null;

//if the item name matches the search parameter, send it up)
if (itemName == searchName)
{
return itemName;
}

if (subitem != null)
{

//spiral down a level
foreach (Item tSearchItem in subitem)
{
tItemName = tSearchItem.LocateItem(searchName);

if (tItemName != null)
break; //exit for if item was found
}
}


//do name logic (use index numbers)
//if LocateItem of the subitems returned nothing and the current item is not a match, return null (not found)
if (tItemName == null && itemName != searchName)
{
return null;
}

//if it's not the item being searched for and the search item was found, change the string and return it up
if (tItemName != null && itemName != searchName)
{
tItemName.Insert(0, itemName + "."); //insert the parent name on the left --> TopItem.SubItem.SubSubItem.SubSubSubItem
return tItemName;
}

//default not found
return null;
}


}

我的问题是是否有更简单的方法来处理列表?关于我应该使用列表还是只使用数组,我一直在脑海中反复考虑。我有一个列表的唯一原因是我不必在每次添加或删除项目时都编写代码来调整数组的大小。

最佳答案

列表听起来很棒。不过,我建议对您的定义进行修改。尝试像这样创建你的类:

public class Item : List<Item>
{
public string Name;
}

如果你制作Item继承自 List<Item>你会自动将它变成一棵树而不需要 subitem字段。

这是我对你的类(class)的完整版本:

public class Item : List<Item>
{
public string Name;

private List<Item> LocateItems(string searchName)
{
if (this.Name == searchName)
return (new [] { this }).ToList();

var result =
this
.Select(s => s.LocateItems(searchName))
.Where(x => x !=null && x.Count > 0)
.FirstOrDefault();

if (result != null)
result.Add(this);

return result;
}

public string LocateItem(string searchName)
{
var items = this.LocateItems(searchName);
if (items == null)
return null;
else
return String.Join(".", items.Select(i => i.Name).Reverse());
}
}

方法LocateItems返回 Item 的列表从 Item 开始匹配并跟随所有父级 Item直到并包括根的实例。

我用这段代码测试过:

var foos = new Item() { Name = "Foo" };
var bars = new Item() { Name = "Bar" };
var qazs = new Item() { Name = "Qaz" };
var wees = new Item() { Name = "Wee" };

foos.Add(bars);
bars.Add(qazs);
foos.Add(wees);

Console.WriteLine(foos.LocateItem("Wee"));
Console.WriteLine(foos.LocateItem("Qaz"));
Console.WriteLine(foos.LocateItem("Bar"));
Console.WriteLine(foos.LocateItem("Foo"));

我得到了这些结果:

Foo.Wee
Foo.Bar.Qaz
Foo.Bar
Foo

关于使用列表的 C# 递归编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12241540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com