How to copy elements from one array to another in C#

by Jagadish Pulakhandam on 10/18/2011 11:45:58 AM
Rated 0 from 0 votes
Brief: Demonstrates on copying elements from one array to another dynamically. In this example, we copy all primes in an array to another array. The target array gets dynamically resized based on number of elements being added.
Posted to: Basics of .NET Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Demonstrates the following:
  • How to declare and initialize arrays
  • How to declare and initialize empty arrays
  • How to resize (increase the size of) an array dynamically
  • How to use Array.Resize in .NET
  • How to copy elements available in one array to another
  • How to copy all prime numbers available in an array to another
Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace PrimeNumbersToFindInArrayAndCopy2Another
{
    class Program
    {
        static void Main(string[] args)
        {
            //some random numbers
            int[] a = { 34, 56, 78, 89, 43, 23, 7, 5, 99, 91, 97 };
 
            //target array to hold only prime numbers
            int[] b = { }; //as we don't know the number of elements, we will not initialize
 
            //traverses through each element in array 'a'
            foreach (int n in a)
            {
                if (isPrime(n))
                {
                    //if a number is prime, place it into array 'b'
                     
                    //the size of array 'b' needs to resized (increase by 1)
                    Array.Resize<int>(ref b, b.Length + 1);
                    //find the last location in b
                    int p = b.Length - 1;
                    //place the number in last location of b
                    b[p] = n;
                }
            }
 
            //display all elements in b
            foreach (int i in b)
            {
                Console.WriteLine(i);
            }
 
            //wait for enter key
            Console.ReadLine();
        }
 
        static bool isPrime(int n)
        {
            bool b = true;
            //1 is always a factor for every number (we start with 2)
            //there can never be a factor of a number which is above the half of the number.
            //ex:for a number 200, the highest factor is 100 (which is just half of 200).
            for (int i = 2; i <= (n / 2); i++)
            {
                //if a factor is found for 'n', it is no longer a prime
                if ((n % i) == 0)
                {
                    b = false;
                    break;
                }
            }
            return b;
        }
    }
}


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following