4

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.

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.

4
  • 7
    In C# 4.0 - you should not create ArrayList but instead use the type-safe List<T> for your lists Commented May 11, 2012 at 9:43
  • This is C#1.1, not C#4.0 code. Commented May 11, 2012 at 9:44
  • Use List<PipeList> - ArrayList is not recommended for any C# code above 1.1, not since generics were introduced. Commented May 11, 2012 at 9:47
  • This question is part of the preliminary test that my company (Xibis, www.xibis.com, we advertise on the right >>) issues to developer candidates before they come and sit a more complicated test in house, to ensure that they have a grounding in basic development skills so we don't waste our time, or the candidates time. To any candidates thinking of using the answers below: we are aware of these answers, so if you submit them as your own you will not be invited for the in house test. Please attempt to answer the questions on your own. Commented May 14, 2012 at 10:06

3 Answers 3

3

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:

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

Sign up to request clarification or add additional context in comments.

2 Comments

it is a shame, since this is posted with the c#-4.0 tag you would think there would be more flexibility if whoever is giving the rules is allowing c#4.
Exactly. Besides using C#1.1 even for learing purposes is kinda pointless for me.
0

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).

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.

2 Comments

The challange is I can only change the "RemoveTheSmallPipes" method. As I have been given as above, I should find the solutions by only changing the method which I have mentioned.
Can you even add another namespace, so you can at least use linq? if so a bit ugly but: '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; }'
0

You could try:

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

4 Comments

The above mentioned example is having problems and as I am new to C#, can you please explan me the error which I am having like follows. "The type or namespace name 'ArrayList' could not be found (are you missing a using directive or an assembly reference?)" and what do I need to do with it.
write at the top of your page Using System.Collections; and using System.Linq; this will include reference
After I have done what you have asked me to do, there is still have got errors like "'System.Collections.ArrayList' does not contain a definition for 'count' and no extension method 'count' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?)" and "'object' does not contain a definition for 'Pipes' and no extension method 'Pipes' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference)" Can you please explain me the reasons of these two errors
The second error couldn't solve with Pipes and how can I solve that? The error was object "'does not contain a definition for 'Pipes' and no extension method 'Pipes' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference)" Please can you explain? Thankx in advance

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.