簡體   English   中英

在C#中附加到字符串數組

[英]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();

            }

        }

這是動物游戲的標准猜測。我在Words[Words.Length] = Input; index out of bounds Words[Words.Length] = Input; ..輸入的新動物和線索也需要在下次我玩游戲時可用。

相反, string []使用System.Collections.Generic中的 List<T>

您可以使用Add方法添加新值,如下所示。

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

如果要在最后放置一個數組,則可以使用ToArray方法。 像這樣。

Words.ToArray();

將字符串添加到有限數組(例如您聲明的數組)中會導致此附加項超出此數組的范圍,即。 您將更多的東西擠進了無法容納該數量的空間。 一旦創建(在編譯時),此大小將固定不變,直到下一次才能更改。

我認為您正在尋找的是列表List<string> ,該List<string>使用list.Add()在運行時輕松動態地操作內容。

讓我知道這是否回答了您的問題,或者您是否需要更多詳細信息。

您可以使用List<string>代替Array 您的代碼引發異常,因為您試圖將元素添加到超出索引的數組中。 我這樣修改了您的代碼;

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

            }

        }

你為什么不使用列表而不是數組。

     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());

        }

    }

請遵循此。

`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;`

暫無
暫無

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

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