在 C# 中创建一个数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10549003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create an Array List in C#
提问by Leo
I have got the problem with creating an array list in c#, Can you please help me. I need to create the array list to remove all the pipes that have lengths lower than 19.
我在 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;
}
}
}
I have created the two classes called Pipe and PipeList as well and I need to place the array list in the "RemoveTheSmallPipes" method. But I am confused now to write the rest. Please help me to remove all the pipes that have lengths lower than 19.
我还创建了两个名为 Pipe 和 PipeList 的类,我需要将数组列表放在“RemoveTheSmallPipes”方法中。但是我现在很困惑要写其余的。请帮我拆下所有长度小于 19 的管道。
采纳答案by lucask
Here's your method without changing anything else:
这是您的方法,无需更改任何其他内容:
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;
}
It's really a shame You cannot change anything else because this code is not up to standards anymore. To show You a difference here's modified C# 4.0 version:
真的很遗憾你不能更改任何其他内容,因为此代码不再符合标准。为了向您展示不同之处,这里修改了 C# 4.0 版本:
Console:
安慰:
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));
}
Classes:
课程:
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>())
{
}
}
As You can see it's quite different, but much more elegant (Linq FTW! :) )
正如您所看到的,它完全不同,但更加优雅(Linq FTW!:))
回答by Zaheer Ahmed
You could try:
你可以试试:
for (i = 0; i < lstPipeTypes.Count; i++) {
((PipesList)lstPipeTypes[i]).Pipes.remove(x => x.Length < 19);
}
回答by Dene B
Agreeing with comment about using List instead of ArrayList, so I changed your code a tad (would of changed it more, but wanted to keep it looking familiar).
同意关于使用 List 而不是 ArrayList 的评论,所以我稍微改变了你的代码(会改变它更多,但想让它看起来很熟悉)。
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IList<PipesList> lstPipeTypes = new List <PipesList>();
lstPipeTypes.Add(new PipesList("PVC Pipes"));
(lstPipeTypes[0]).Pipes.Add(new Pipe("The blue pipe", 12));
(lstPipeTypes[0]).Pipes.Add(new Pipe("The red pipe", 15));
(lstPipeTypes[0]).Pipes.Add(new Pipe("The silver pipe", 6));
(lstPipeTypes[0]).Pipes.Add(new Pipe("The green pipe", 52));
lstPipeTypes.Add(new PipesList("Iron Pipes"));
(lstPipeTypes[1]).Pipes.Add(new Pipe("The gold pipe", 9));
(lstPipeTypes[1]).Pipes.Add(new Pipe("The orange pipe", 115));
(lstPipeTypes[1]).Pipes.Add(new Pipe("The pink pipe", 1));
lstPipeTypes.Add(new PipesList("Chrome Pipes"));
(lstPipeTypes[2]).Pipes.Add(new Pipe("The grey pipe", 12));
(lstPipeTypes[2]).Pipes.Add(new Pipe("The black pipe", 15));
(lstPipeTypes[2]).Pipes.Add(new Pipe("The white pipe", 19));
(lstPipeTypes[2]).Pipes.Add(new Pipe("The brown pipe", 60));
(lstPipeTypes[2]).Pipes.Add(new Pipe("The peach pipe", 16));
RemoveTheSmallPipes(lstPipeTypes);
}
public static IList<PipesList> RemoveTheSmallPipes(IList<PipesList> lstPipeTypes)
{
foreach (var pipesList in lstPipeTypes)
{
pipesList.Pipes = pipesList.Pipes.Where(p => p.length >= 19).ToList();
}
return lstPipeTypes;
}
}
class PipesList
{
public string pipeType;
public IList<Pipe> Pipes;
public PipesList(string newBoxType)
{
pipeType = newBoxType;
Pipes = new List <Pipe>();
}
}
class Pipe
{
public string name;
public float length;
public Pipe(string newName, float newLength)
{
this.name = newName;
this.length = newLength;
}
}
}
I would also suggest you taking out the hard-coded value of 19, and using a const or something, or perhaps as a parameter to the method.
我还建议您取出 19 的硬编码值,并使用 const 或其他东西,或者作为方法的参数。

