C# | How to insert an element in an Array? (original) (raw)
Last Updated : 28 Aug, 2019
An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C#. Let's say we have an array and we want to insert an element at a specific position in this array. Here's how to do it.
- First get the element to be inserted, say x
- Then get the position at which this element is to be inserted, say pos
- Create a new array with the size one greater than the previous size
- Copy all the elements from previous array into the new array till the position pos
- Insert the element x at position pos
- Insert the rest of the elements from the previous array into the new array after the pos CSharp `
// C# program to insert an // element in an array using System;
public class GFG {
// Main Method
static public void Main()
{
int n = 10;
int[] arr = new int[n];
int i;
// initial array of size 10
for (i = 0; i < n; i++)
arr[i] = i + 1;
// print the original array
for (i = 0; i < n; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
// element to be inserted
int x = 50;
// position at which element
// is to be inserted
int pos = 5;
// create a new array of size n+1
int[] newarr = new int[n + 1];
// insert the elements from the
// old array into the new array
// insert all elements till pos
// then insert x at pos
// then insert rest of the elements
for (i = 0; i < n + 1; i++) {
if (i < pos - 1)
newarr[i] = arr[i];
else if (i == pos - 1)
newarr[i] = x;
else
newarr[i] = arr[i - 1];
}
// print the updated array
for (i = 0; i < n + 1; i++)
Console.Write(newarr[i] + " ");
Console.WriteLine();
}
}
`
Output:
1 2 3 4 5 6 7 8 9 10 1 2 3 4 50 5 6 7 8 9 10
Similar Reads
- C# | Insert an element into the ArrayList at the specified index ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayLis 3 min read
- C# | Copy the Stack to an Array Stack.CopyTo(T[], Int32) Method is used to copy the Stack to an existing 1-D Array which starts from the specified array index. Properties: The capacity of a Stackis the number of elements the Stack can hold. As elements are added to a Stack , the capacit 2 min read
- C# | ArrayList.InsertRange() Method ArrayList.InsertRange(Int32, ICollection) Method in C# is used to insert the elements from a collection into the ArrayList at a specified index. That is the inserted element belongs to a collection(i.e. queue etc). Syntax: public virtual void InsertRange (int index, ICollection element); Parameters: 3 min read
- C# | How to insert the elements of a collection into the List at the specified index List.InsertRange(Int32, IEnumerable) Method is used to insert the elements of a collection into the List at the specified index. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid 3 min read
- C# | How to create a Stack Stack() constructor is used to initialize a new instance of the Stack class which will be empty and will have the default initial capacity. Stack represents a last-in, first out collection of object. It is used when you need last-in, first-out access to items. When you add an item in the list, it is 2 min read
- C# | Creating an ArrayList having specified initial capacity ArrayList(Int32) constructor is used to initialize a new instance of the ArrayList class which will be empty and will have the specified initial capacity. ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also all 3 min read
- C# Jagged Arrays A jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows 4 min read
- C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe 8 min read
- How to Insert an Element into an Array of Structs at a Specific Position in C? In C, structs allow the users to create user-defined data types which can be used to store data of different types in a single unit. In many use cases, we might use an array of structs to store the structs in contiguous memory locations to access them sequentially. In this article, we will learn how 3 min read
- C Program to Insert an Element in an Array In this article, we will learn how to insert an element into an array in C.C arrays have a fixed size, so we cannot dynamically increase their memory. However, we can insert an element if the array already have enough memory space to accommodate the new elementsInsert Element at Specific PositionTo 3 min read