在C#中创建一个数组列表

4

我在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的管道。


7
在C# 4.0中,您应该 不要 创建 ArrayList ,而是改用类型安全的 List<T> 来管理列表。 - marc_s
这是C#1.1的代码,不是C#4.0的代码。 - Sergey Kalinichenko
@dasblinkenlight - 检查标签。 - Euphoric
请使用 List<PipeList> - 自从引入泛型以来,ArrayList 不建议在任何 C# 1.1 以上的代码中使用。 - Oded
这个问题是我的公司 Xibis (www.xibis.com,我们在右侧广告>>上做广告) 在候选开发人员来参加更复杂的内部测试之前,向他们提出的初步测试的一部分。确保他们有基本的开发技能,以便我们不浪费时间或候选人的时间。对于任何考虑使用以下答案的候选人:我们知道这些答案,所以如果您将它们提交为您自己的答案,则不会被邀请参加内部测试。请尽力自己回答这些问题。 - Ian Newson
有没有完整源代码的最终解决方案? - Kiquenet
3个回答

3

这里是不改变任何其他内容的方法:

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#-4.0标签,您可能会认为如果制定规则的人允许c#4,则会有更多的灵活性。 - Dene B
没错。对我来说,即使是为了学习目的,使用C#1.1也有点毫无意义。 - lucask

0
你可以尝试一下:
for (i = 0; i < lstPipeTypes.Count; i++) {
    ((PipesList)lstPipeTypes[i]).Pipes.remove(x => x.Length < 19);     
}

上述示例存在问题,由于我是C#的新手,您能否请解释一下我遇到的错误,如下所示:“找不到类型或命名空间名称'ArrayList'(您是否缺少使用指令或程序集引用?)”,我需要怎么做。 - Leo
请在页面顶部写入“Using System.Collections;”和“Using System.Linq;”,这将包含引用。 - Zaheer Ahmed
完成您所要求的任务之后,仍然存在以下错误: "'System.Collections.ArrayList'不包含“count”的定义,也找不到接受类型为'System.Collections.ArrayList'的第一个参数的扩展方法'count'(是否缺失using指令或程序集引用?)" 和 "'object'不包含“Pipes”的定义,也找不到接受类型为'object'的第一个参数的扩展方法'Pipes'(是否缺失using指令或程序集引用?)" 请问您能解释一下这两个错误的原因吗? - Leo
第二个错误无法使用Pipes解决,我该如何解决?错误是“对象“'不包含对' Pipes '的定义,也没有接受类型为' object '的第一个参数的扩展方法' Pipes '可以找到(您是否缺少使用指令或程序集引用)”请问能否解释一下?提前感谢。 - Leo

0
同意使用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;
        }
    }


}

我还建议您将硬编码的值19拿出来,使用常量或类似的东西,或者作为该方法的参数。

挑战在于我只能更改“RemoveTheSmallPipes”方法。如上所述,我应该通过仅更改我提到的方法来找到解决方案。 - Leo
你能添加另一个命名空间,这样至少可以使用linq吗?如果可以,虽然有点丑陋,但是:'foreach (PipesList pipeType in lstPipeTypes) { var temp = new ArrayList(); foreach (var pipe in pipeType.Pipes.Cast<Pipe>().Where(pipe => pipe.length >= 19)) { temp.Add(pipe); } pipeType.Pipes = temp; }' - Dene B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接