如何将元素插入现有的字符串数组中 [英] How to insert elements into an existing string array

查看:112
本文介绍了如何将元素插入现有的字符串数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不覆盖现有值的情况下将新元素插入现有数组。



How to insert new elements into an existing array without overwriting the existing values.

Console.Write("Enter the size of the array: ");
           string inputSize = Console.ReadLine();
           int stringSize;
           int.TryParse(inputSize, out stringSize);

           string[] stringArray=new string[stringSize];
           Console.WriteLine("Enter elements one by one\n");
           for (int index = 0; index < stringSize; index++)
           {
               stringArray[index] = Console.ReadLine();
           }
           Console.WriteLine("\n\nGiven Array\n");
           foreach (string strings in stringArray)
           {
               Console.WriteLine(strings);
           }
           Console.Write("\nEnter String: ");

           string input = Console.ReadLine();
           int TextIndex = Array.FindIndex(stringArray, m => m == input);

           Console.Write("\nRequired string index: {0}\n",TextIndex);
           int newSize=stringSize+1;
           Array.Resize(ref stringArray,newSize);


           int nIndex=TextIndex+1;
           while(nIndex>TextIndex)
           {
               if (nIndex < newSize)
               {
                   stringArray[TextIndex + 1] = stringArray[TextIndex];
                   nIndex++;
               }

              break;
           }
           Console.Write("\nEnter new string: ");
           stringArray[TextIndex] = Console.ReadLine();







           foreach(string newElements in stringArray)
           {
               Console.WriteLine(newElements);
           }






           Console.ReadLine();











如果我们输入onetwothreefivesix



我们应该在运行时添加4并将其添加到数组和数组大小增加。



那么结果数组应该是

onetwothreefour 五六






If we give input as "one" "two" "three" "five" "six"

We should add "four" at runtime and add it in the array and array size size gets incremented.

Then the resulting array should be
"one" "two" "three" "four" "five" "six"

推荐答案

你会多少次一次又一次地提出同样的问题?

你的问题是什么?到底?你都尝试了些什么 ?你被困在哪里?

你的代码在哪里?



如果我没弄错的话,我'已经解决了2-3次类似的问题。



-KR
How many time are you gonna put up the same question again and again ?
What is your problem exactly ? What have you tried so far ? Where have you been stuck ?
Where is your code ?

If I'm not mistaken, I've solved 2-3 times similar kind of problem of yours.

-KR


没什么可以插入数组或从数组中删除。在这方面,数组是不可变的。所有这样做的方法只是创建一个全新的数组实例并复制所有数据。改为使用 System.Collections.Generic.Array<>



-SA
Nothing can be inserted into array or removed from array. Arrays are immutable in this respect. All methods doing so just create a brand new array instance and copy all data. Use System.Collections.Generic.Array<> instead.

—SA


你可以读到这个文章它会帮助你解决你的这个问题。



快乐编码:)
you can read this article it will help you to do this problem of yours.

Happy coding :)


这篇关于如何将元素插入现有的字符串数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆