Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to insert new elements into an existing array without overwriting the existing values.

C#
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"
Posted
Updated 17-Feb-14 20:07pm
v2

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
 
Share this answer
 
Comments
KUMAR619 18-Feb-14 2:07am    
@KR I have added my code.
Please go through it and make the array as per the output
KUMAR619 18-Feb-14 2:23am    
@KR
Please look at my code any get me the required output
Sergey Alexandrovich Kryukov 18-Feb-14 2:34am    
My 5 for raising this problem. This is too annoying. Would you report it on abuse report forum? And mention the rudeness. "...get me the required output", aha. We cannot help people behaving this way, it's just useless.
—SA
Sergey Alexandrovich Kryukov 18-Feb-14 2:47am    
Re-posted again! This is intolerable.
—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
 
Share this answer
 
you can read this article it will help you to do this problem of yours.

Happy coding :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-14 2:38am    
Bad idea. The question is about array. Array cannot insert/remove anything. And ArrayList you advised should never be used, it is only good enough in legacy code. If you would use it, it's not good for you. Use System.Collections.Generic. Non-generic (non-specialized) collections were rendered obsolete as early as of .NET v.2, VS.2005.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900