locked
Arrays and expanding arrays RRS feed

  • Question

  • Hi!
    So I was wondering in the code below under Car[] foundCars = new Car[0];, I would like to in the end of the block return the value foundcars but I don't know what to write in between. Any suggestions? ( In the code you can see it as a comment that says What to write here??)
    And then under return foundcars I would like to write a method to expand the array of foundcars. But the method has to be called in from public static Car[] GetCarsByBrand(string brand). 
    So how do you write an extended array of foundcars while calling in the method; public static Car[] GetCarsByBrand(string brand)?
               
     using System;
    
    namespace NissesBil
    {
        public class Program
        {
            
            public static Car[] cars = Randomizer.GenerateCar(300);
    
            
            public static Car[] GetCarsByBrand(string brand)
            {
                Car[] foundCars = new Car[0];
    
                // What to write here??
                
                return foundCars;
            }
    
            
            //Write a method for extending the array foundcars, 
            //the method must be called from another method, GetCarsByBrand
         
    
                
    • Edited by Billba Monday, February 6, 2012 8:42 PM Forgot to delete a small piece of txt in the ending, so it wouldnt become confusing
    Monday, February 6, 2012 8:40 PM

Answers

  • It's still not clear to me what you're trying to accomplish.  I wrote in my first reply that you cannot extend arrays, but I haven't gotten any clarification of your intent.  That said, I can assume intent based on your method name.  Based on that I think your code should be:

    public static Car[] GetCarsByBrand(string brand)
    {
       var carQuery = from c in cars
                      where c.brand == brand
                      select c;
    
       return carQuery.ToArray();
    }

    By the way, you should mark the Randomizer class as static.  Classes with only static members should be marked static so that they cannot be instantiated.


    • Edited by Anthony Trudeau Tuesday, February 7, 2012 1:41 PM
    • Proposed as answer by Anthony Trudeau Tuesday, February 14, 2012 2:13 PM
    • Marked as answer by Lie You Tuesday, February 21, 2012 2:10 AM
    Tuesday, February 7, 2012 1:41 PM
  • I see your point. However I have a public class that is named Car and under this I have a Randomizer to pull upp some random brands. I would really love to see an example. Hope this helps
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace NissesBil
    {
        public class Car
        {
            public string regnr = "";
            public int year = DateTime.Today.Year;
            public string brand = "";
            public string model = "";
    
            public override string ToString()
            {
                return brand + " " + model + ", " + year + " (" + regnr + ")";
            }
        }
    }
    

    namespace NissesBil
    {
        class Randomizer
        {
            static Random rand = new Random();
            public static string[] brand = { "Volvo", "Renault", "Fiat", "Saab", "Opel", "Ford", "VW", "Audi" };
    
            public static Car[] GenerateCar(int antal)
            {
                Car[] list = new Car[antal];
    
                for (int i = 0; i < antal; i++)
                {
                    list[i] = RandomBil();
                }
                return list;
            }
    
            public static Car RandomBil()
            {
                string initials = "ABCDEFGHIJKLMNOPQRSTUVXYZ";
    
                Car bil = new Car();
                bil.year = rand.Next(1970, DateTime.Today.Year + 1);
                bil.model = initials[rand.Next(initials.Length)].ToString() + (rand.Next(1, 10) * 10);
               
                bil.brand = märke[rand.Next(märke.Length)];
                string regnr = "";
    
                for (int i = 0; i < 3; i++)
                {
                    regnr += initials[rand.Next(initials.Length)];
                }
                for (int i = 0; i < 3; i++)
                {
                    regnr += rand.Next(10);
                }
                bil.regnr = regnr;
                return bil;
            }
        }
    }
    

    • Marked as answer by Lie You Tuesday, February 21, 2012 2:10 AM
    Tuesday, February 7, 2012 8:51 AM

All replies

  • You cannot extend arrays.  You should use the ArrayList or strongly typed List<T> when your size is going to vary.  Alternately, you can create a new array with the new size, use Array.Copy to copy the old array elements to the new array, and then return the new array.

    I'd give you a sample, but it's not clear to me how your method is going to be used.  You have the field cars that isn't used in your example.  And there's no context on how GetCarsByBrand is being used.  From your example it looks like GetCarsByBrand is just going to return a zero-length array.

    Monday, February 6, 2012 9:09 PM
  • You want to use a List<Car>.  It is by far the best option for what you want to do.  While it's good that Anthony provided the alternatives, you don't actually want to use them in your case.

    If it's absolutely imperative that you return an Array when you're done, use a List<Car> for the calculation and then use the LINQ .ToArray() method to convert to the List to an Array.

    Monday, February 6, 2012 9:17 PM
  • Does it have to be an array?

    Why not use a List? Then it's just:

     

     

    myList.Add(new Car());
    

     

    Alternatively you can use an ArrayList, which is dynamically increased as required.

     

    http://msdn.microsoft.com/en-us/library/7x4b0a97(vs.71).aspx

     

    If your object starts as Car[], you can convert to List<Car> with

     

     

    var Cars = new List<Car>(foundCars);

     

    And back again with Cars.ToArray<Car>()

     

                Car[] carsArray1 = new Car[] { new car(), new Car() };
    
                List<Car> Cars = new List<Car>();
    
                Car[] carsArray2 = Cars.ToArray<Car>();
    

     

    Hope that helps!

     

    Regards,
    Pedro

     

     

     


    If you find my post helpful, please remember to "Mark As Answer" and/or "Vote as Helpful"
    Monday, February 6, 2012 9:18 PM
    Answerer
  • I see your point. However I have a public class that is named Car and under this I have a Randomizer to pull upp some random brands. I would really love to see an example. Hope this helps
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace NissesBil
    {
        public class Car
        {
            public string regnr = "";
            public int year = DateTime.Today.Year;
            public string brand = "";
            public string model = "";
    
            public override string ToString()
            {
                return brand + " " + model + ", " + year + " (" + regnr + ")";
            }
        }
    }
    

    namespace NissesBil
    {
        class Randomizer
        {
            static Random rand = new Random();
            public static string[] brand = { "Volvo", "Renault", "Fiat", "Saab", "Opel", "Ford", "VW", "Audi" };
    
            public static Car[] GenerateCar(int antal)
            {
                Car[] list = new Car[antal];
    
                for (int i = 0; i < antal; i++)
                {
                    list[i] = RandomBil();
                }
                return list;
            }
    
            public static Car RandomBil()
            {
                string initials = "ABCDEFGHIJKLMNOPQRSTUVXYZ";
    
                Car bil = new Car();
                bil.year = rand.Next(1970, DateTime.Today.Year + 1);
                bil.model = initials[rand.Next(initials.Length)].ToString() + (rand.Next(1, 10) * 10);
               
                bil.brand = märke[rand.Next(märke.Length)];
                string regnr = "";
    
                for (int i = 0; i < 3; i++)
                {
                    regnr += initials[rand.Next(initials.Length)];
                }
                for (int i = 0; i < 3; i++)
                {
                    regnr += rand.Next(10);
                }
                bil.regnr = regnr;
                return bil;
            }
        }
    }
    

    • Marked as answer by Lie You Tuesday, February 21, 2012 2:10 AM
    Tuesday, February 7, 2012 8:51 AM
  • It's still not clear to me what you're trying to accomplish.  I wrote in my first reply that you cannot extend arrays, but I haven't gotten any clarification of your intent.  That said, I can assume intent based on your method name.  Based on that I think your code should be:

    public static Car[] GetCarsByBrand(string brand)
    {
       var carQuery = from c in cars
                      where c.brand == brand
                      select c;
    
       return carQuery.ToArray();
    }

    By the way, you should mark the Randomizer class as static.  Classes with only static members should be marked static so that they cannot be instantiated.


    • Edited by Anthony Trudeau Tuesday, February 7, 2012 1:41 PM
    • Proposed as answer by Anthony Trudeau Tuesday, February 14, 2012 2:13 PM
    • Marked as answer by Lie You Tuesday, February 21, 2012 2:10 AM
    Tuesday, February 7, 2012 1:41 PM
  • Hi Billba,

     

      Welcome to MSDN Forum Support. Thank you for sharing your solid solution.

    Sincerely,

    Jason Wang



    orichisonic http://blog.csdn.net/orichisonic

    Wednesday, February 8, 2012 1:12 AM