[REPOST]在字符串数组中添加元素 [英] [REPOST] Add elements in string array

查看:94
本文介绍了[REPOST]在字符串数组中添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





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







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"

推荐答案

我已经回答:如何将元素插入现有的字符串数组 [ ^ ]。



根据您的行为,您应该离开此网站。帮助你是完全没用的。



-SA
I already answered: How to insert elements into an existing string array[^].

With your behavior, you should rather leave this site. Helping you is totally useless.

—SA


这篇关于[REPOST]在字符串数组中添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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