gpt4 book ai didi

c# - 在 C# 中创建数组列表

转载 作者:行者123 更新时间:2023-11-30 12:14:01 25 4
gpt4 key购买 nike

我在用 C# 创建数组列表时遇到了问题,你能帮帮我吗?我需要创建数组列表以删除所有长度小于 19 的管道。

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList lstPipeTypes = new ArrayList();

lstPipeTypes.Add(new PipesList("PVC Pipes"));
((PipesList)lstPipeTypes[0]).Pipes.Add(new Pipe("The blue pipe", 12));
((PipesList)lstPipeTypes[0]).Pipes.Add(new Pipe("The red pipe", 15));
((PipesList)lstPipeTypes[0]).Pipes.Add(new Pipe("The silver pipe", 6));
((PipesList)lstPipeTypes[0]).Pipes.Add(new Pipe("The green pipe", 52));

lstPipeTypes.Add(new PipesList("Iron Pipes"));
((PipesList)lstPipeTypes[1]).Pipes.Add(new Pipe("The gold pipe", 9));
((PipesList)lstPipeTypes[1]).Pipes.Add(new Pipe("The orange pipe", 115));
((PipesList)lstPipeTypes[1]).Pipes.Add(new Pipe("The pink pipe", 1));

lstPipeTypes.Add(new PipesList("Chrome Pipes"));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The grey pipe", 12));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The black pipe", 15));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The white pipe", 19));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The brown pipe", 60));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The peach pipe", 16));


RemoveTheSmallPipes(lstPipeTypes);
}


public static ArrayList RemoveTheSmallPipes(ArrayList lstPipeTypes)
{

//should remove all pipes that have lengths lower than 19.

return lstPipeTypes;

}
}

class PipesList
{
public string pipeType;
public ArrayList Pipes;

public PipesList(string newBoxType)
{
pipeType = newBoxType;
Pipes = new ArrayList();
}
}

class Pipe
{
public string name;
public float length;

public Pipe(string newName, float newLength)
{
this.name = newName;
this.length = newLength;
}
}


}

我还创建了两个名为 Pipe 和 PipeList 的类,我需要将数组列表放在“RemoveTheSmallPipes”方法中。但我现在对写剩下的部分感到困惑。请帮我移除所有长度小于 19 的管道。

最佳答案

这是您的方法,无需更改任何其他内容:

public static ArrayList RemoveTheSmallPipes(ArrayList lstPipeTypes)
{

for (int i = 0; i < lstPipeTypes.Count; i++)
{
ArrayList pipesToRemove = new ArrayList();
int pipeCount = ((PipesList)lstPipeTypes[i]).Pipes.Count;
for (int j = 0; j < pipeCount; j++)
{
if (((Pipe)((PipesList)lstPipeTypes[i]).Pipes[j]).length < 19)
{
pipesToRemove.Add(j);
}
}

for (int k = 0; k < pipesToRemove.Count; k++)
{
((PipesList)lstPipeTypes[i]).Pipes.RemoveAt((int)pipesToRemove[k]);
}
}

return lstPipeTypes;
}

真可惜你不能改变任何其他东西,因为这段代码已经不符合标准了。为了向您展示这里修改后的 C# 4.0 版本的不同之处:

控制台:

static void Main(string[] args)
{
var pipeTypes = new List<PipePile>();

pipeTypes.Add(new PipePile("PVC Pipes", new List<Pipe>
{
new Pipe { Name = "The blue pipe", Length = 12 },
new Pipe { Name = "The red pipe", Length = 15 },
new Pipe { Name = "The silver pipe", Length = 6 },
new Pipe { Name = "The green pipe", Length = 52 }
}));

pipeTypes.Add(new PipePile("Iron Pipes", new List<Pipe>
{
new Pipe { Name = "The gold pipe", Length = 9 },
new Pipe { Name = "The orange pipe", Length = 115 },
new Pipe { Name = "The pink pipe", Length = 1 }
}));

pipeTypes.Add(new PipePile("Chrome Pipes", new List<Pipe>
{
new Pipe { Name = "The grey pipe", Length = 12 },
new Pipe { Name = "The black pipe", Length = 15 },
new Pipe { Name = "The white pipe", Length = 19 },
new Pipe { Name = "The brown pipe", Length = 60 },
new Pipe { Name = "The peach pipe", Length = 16 }
}));

// Remove all pipes with length longer than 19
pipeTypes.ForEach(pile => pile.Pipes.RemoveAll(pipe => pipe.Length > 19));
}

类:

public class Pipe
{
public string Name { get; set; }
public float Length { get; set; }
}

public class PipePile
{
public string PipeType { get; set; }
public List<Pipe> Pipes { get; set; }

public PipePile(string pipeType, List<Pipe> pipes)
{
PipeType = pipeType;
Pipes = pipes;
}

public PipePile(): this("", new List<Pipe>())
{
}
}

如您所见,它非常不同,但更加优雅(Linq FTW!:))

关于c# - 在 C# 中创建数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10549003/

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