How to copy elements in an array to an ArrayList or List

by Jagadish Pulakhandam on 10/18/2011 11:52:30 AM
Rated 0 from 0 votes
Brief: Demonstrates on copying elements available in an array to an ArrayList or List. In this example, we copy all prime numbers available in main array to an ArrayList and a List. We also traverse through all elements in ArrayList and List to display their content.
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 initialize and parse through Arrays
  • How to instantiate ArrayList
  • How to instantiate List<int> (List of integers)
  • How to add items to ArrayList and List<int> dynamically
  • How to copy items from an array to ArrayList and List
  • How to copy all prime numbers available in an array to ArrayList and List
  • How to traverse through all elements in ArrayList and List
Source Code:
01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.namespace PrimeNumbersToFindInArrayAndCopy2ArrayList
07.{
08.    class Program
09.    {
10.        static void Main(string[] args)
11.        {
12.            //some random numbers
13.            int[] a = { 34, 56, 78, 89, 43, 23, 7, 5, 99, 91, 97 };
14. 
15.            //method 1: array list to hold only prime numbers
16.            System.Collections.ArrayList al = new System.Collections.ArrayList();
17. 
18.            //method 2: list<int> to hold only prime numbers
19.            List<int> lst = new List<int>();
20.             
21.            //traverses through each element in array 'a'
22.            foreach (int n in a)
23.            {
24.                if (isPrime(n))
25.                {
26.                    //if a number is prime,
27.                     
28.                    //place it into array list
29.                    al.Add(n);
30. 
31.                    //place it into list
32.                    lst.Add(n);
33.                }
34.            }
35. 
36.            //display all elements from "al" (ArrayList)
37.            Console.WriteLine("ArrayList");
38.            Console.WriteLine("----------");
39.            foreach (int i in al)
40.            {
41.                Console.WriteLine(i);
42.            }
43.            Console.WriteLine();
44. 
45. 
46.            //display all elements from "lst" (List<int>)
47.            Console.WriteLine("List");
48.            Console.WriteLine("----------");
49.            foreach (int i in lst)
50.            {
51.                Console.WriteLine(i);
52.            }
53.            Console.WriteLine();
54. 
55.            //wait for enter key
56.            Console.ReadLine();
57.        }
58. 
59.        static bool isPrime(int n)
60.        {
61.            bool b = true;
62.            //1 is always a factor for every number (we start with 2)
63.            //there can never be a factor of a number which is above the half of the number.
64.            //ex:for a number 200, the highest factor is 100 (which is just half of 200).
65.            for (int i = 2; i <= (n / 2); i++)
66.            {
67.                //if a factor is found for 'n', it is no longer a prime
68.                if ((n % i) == 0)
69.                {
70.                    b = false;
71.                    break;
72.                }
73.            }
74.            return b;
75.        }
76.    }
77.}


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