簡體   English   中英

在C#中創建一個數組列表

[英]Create an Array List in C#

我在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!:))

你可以嘗試:

for (i = 0; i < lstPipeTypes.Count; i++) {
    ((PipesList)lstPipeTypes[i]).Pipes.remove(x => x.Length < 19);     
}

同意關於使用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的硬編碼值,並使用const或其他東西,或者作為方法的參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM