1

I need to know how to dynamically re-size an array in C#. In the method that I have written below, I need to be able to return an array that only contains the numbers that were input by the user up to 8 numbers. So if the user decides they only want to enter 3 numbers, the array should only contain 3 numbers, not 8.

Now I know that an array needs to include a size when instantiated. So how do I get around this without using a list? Is there a way to re-size the array after the loop is done?

Thanks in advance.

        static int[] fillArray()
    {
        int[] myArray;
        myArray = new int[8];
        int count = 0;
        do
        {
            Console.Write("Please enter a number to add to the array or \"x\" to stop: ");
            string consoleInput = Console.ReadLine();
            if (consoleInput == "x")
            {
                Array.Resize(ref myArray, count);
                return myArray;
            }
            else
            {
                myArray[count] = Convert.ToInt32(consoleInput);
                ++count;
            }

        } while (count < 8);

        Array.Resize(ref myArray, count);
        return myArray;

    }
2
  • WHy not use a resizable collection type like ArrayList or List? Commented Mar 1, 2011 at 0:27
  • A List<> is nothing but an array with a Count and a Capacity property. Implement your own. Commented Mar 1, 2011 at 1:01

3 Answers 3

7

You could use a List<int> during your method logic and then return myIntList.ToArray();

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

9 Comments

... or even make fillArray return IEnumerable<int>.
Yes, I could do that. But I want to accomplish this without using a list.
@antisanity - lol I was thinking that too - it's the only time Occam's Razor goes out the window...
Well yes because that is what the homework assignment requires, otherwise I'd have just used a list and been done with the assignment 3 days ago. I'm just stumped as how to do this otherwise. I asked the teacher why I'd ever want to do this in real life and he replied " An array allows you to have random access to any element. A list doesn't allow that, unless each element is indexed, in which case it is probably implemented as an array." Sooo I'm stuck.
Also I do want to point out, I'm not asking you to fix my code, or write it for me. I just need someone to point me in the right direction and maybe tell me why I should do it that way. If I don't figure it out soon, I'm just going to go with creating a list and converting it to an array before I return it method.
|
1

Typically for this type of application you'll want to use a List. And if you really need an array you can use the ToArray method but reconsider if an array is really what you want. Usually a List is used for a dynamically sized collection instead of an array.

You could always modify your code as follows:

static int[] fillArray()
{
    List<int> list = new List<int>();

    do
    {
        Console.Write("Please enter a number to add to the array or \"x\" to stop: ");
        string consoleInput = Console.ReadLine();

        if (consoleInput == "x")
        {
            return list.ToArray();
        }
        else
        {
            list.Add(Convert.ToInt32(consoleInput));
        }

    } while (count < 8);

    return list.ToArray();
}

But as I mentioned before, really reconsider changing your method to return a List and use the List in your calling code.

Comments

0

Well you could implement your own method accepting the array and new size as parameters that would create new array, deep-copy the elements and then assign to your array. Array.Resize() does the same, you can watch it with disassembler

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.