简体   繁体   中英

appending to a string array in C#

    string Input = "";
    string[] Words = { "elephant", "lion" };
    string[] Clues = { "Has trunk?", "Is gray?", "Is yellow?", "Has mane?"};

.........

        Console.WriteLine("Do you want to add you own animal? y/n ? \n");
        Input = Console.ReadLine();
        if (Input == "Y" || Input == "y")
        {
            Console.WriteLine("Enter an animal name: \n");
            //Array.Resize(ref Words, Words.Length + 1);
            Input = Console.ReadLine();
            Words[Words.Length] = Input;


            Console.WriteLine("Enter 2 clues \n");
            for (int i = 1; i <=2 ; i++)
            {
                Console.WriteLine("Clue" + i + ":");
                Clues[Clues.Length] = Console.ReadLine();

            }

        }

This is the standard guess the animal game.. I am getting an index out of bounds at line Words[Words.Length] = Input; .. the new animal and clues entered also needs to be available the next time i play the game..

Instead string [] use List<T> from System.Collections.Generic

And you can use Add method to add new value like this.

Console.WriteLine("Enter an animal name: \n");
Input = Console.ReadLine();
Words.Add(Input);

And if want an array at the end, you can use ToArray method. Like this.

Words.ToArray();

Adding a string to a finite array such as the one you have declared causes this additional item to be beyond the bounds of this array, ie. you are squeezing more things into a space that cannot hold that amount. Once created (at compilation) this size is fixed and can't be changed until next time.

I think what you are looking for are lists List<string> that use list.Add() to easily manipulate content dynamically during runtime.

Let me know if that answers your question or if you require more details.

You can use List<string> instead of Array . Your code throw exception because you are trying to add element to an Array in outranged index. I modified your code like this;

        string Input = "";
        var Words = new List<string> { "elephant", "lion" };
        var Clues = new List<string> { "Has trunk?", "Is gray?", "Is yellow?", "Has mane?" };
        Console.WriteLine("Do you want to add you own animal? y/n ? \n");
        Input = Console.ReadLine();
        if (Input == "Y" || Input == "y")
        {
            Console.WriteLine("Enter an animal name: \n");
            //Array.Resize(ref Words, Words.Length + 1);
            Input = Console.ReadLine();
            Words.Add(Input);


            Console.WriteLine("Enter 2 clues \n");
            for (int i = 1; i <= 2; i++)
            {
                Console.WriteLine("Clue" + i + ":");
                var clueInput = Console.ReadLine();
                Clues.Add(clueInput);

            }

        }

why don't you use List rather Array.

     string Input = "";
    List<string> Words = new List<string>(){ "elephant", "lion" };
    List<string>  Clues =new List<string>() { "Has trunk?", "Is gray?", "Is yellow?", "Has mane?"};

 Console.WriteLine("Do you want to add you own animal? y/n ? \n");
    Input = Console.ReadLine();
    if (Input.toLower() == "y")
    {
        Console.WriteLine("Enter an animal name: \n");
        //Array.Resize(ref Words, Words.Length + 1);
        Input = Console.ReadLine();
        Words.Add(Input);


        Console.WriteLine("Enter 2 clues \n");
        for (int i = 1; i <=2 ; i++)
        {
            Console.WriteLine("Clue" + i + ":");
            Clues.Add(Console.ReadLine());

        }

    }

please follow this.

`Console.WriteLine("Enter an animal name: \n");
//Hear Words length is 2 and Data (0 = elephant,1 = lion)
Array.Resize(ref Words, Words.Length + 1);
//Hear Words length is 3 and Data (0 = elephant,1 = lion,2 = null)
Input = Console.ReadLine();
//if you write Words[Words.Length] means you tried to access 3 index which is not available.
//You should write this.
Words[Words.Length - 1] = Input;`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM