Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace InsertInPosition
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] items = { 2, 13, 0, 0, 20, 0, 0, 0 };
- int position = 2;
- int item = 200;
- // insert *item into position *position
- // there is two solutiion
- // 1 - discard the last item when inserting a new itme in middle of the list
- // 2 - resizing the array when inserting a new item in middle of the list
- // solution one:
- int len = items.Length;
- // store the current value in position
- int t = items[position];
- for (int i = position; i + 1 < len; i++)
- {
- items[i] = item;
- item = t;
- if (i + 1 < len)
- {
- t = items[i + 1];
- }
- }
- DisplayArray(items);
- Console.WriteLine("done");
- Console.ReadLine();
- }
- static void DisplayArray(int[] items)
- {
- foreach (var item in items)
- {
- Console.Write(item + ",");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement