C# Generics example

using System;

class Printer
{

    /**
    *    Name: PrintArray
    *    Print each element of the generic array on a new line. Do not return anything.
    *    @param A generic array
    **/
    // Write your code here

     static void PrintArray<T> (System.Collections.Generic.IList<T> coll)
    {
        foreach (T item in coll)
        {
            Console.WriteLine(item.ToString() + " ");
        }
    }

    static void Main(string[] args)
    {
        int n = Convert.ToInt32(Console.ReadLine());
        int[] intArray = new int[n];
        for (int i = 0; i < n; i++)
        {
            intArray[i] = Convert.ToInt32(Console.ReadLine());
        }
        
        n = Convert.ToInt32(Console.ReadLine());
        string[] stringArray = new string[n];
        for (int i = 0; i < n; i++)
        {
            stringArray[i] = Console.ReadLine();
        }
        
        PrintArray<Int32>(intArray);
        PrintArray<String>(stringArray);
    }
}


Compiler Message
Success
Input (stdin)
Download
  • 3
  • 1
  • 2
  • 3
  • 2
  • Hello
  • World
Expected Output
Download
  • 1
  • 2
  • 3
  • Hello
  • World

留言

這個網誌中的熱門文章

香港袐密行動

要老是忘記, 我更記不起