stack示例_C.示例中的Stack.CopyTo()方法

stack示例

C#Stack.CopyTo()方法 (C# Stack.CopyTo() method)

Stack.CopyTo() method is used to copy the stack elements/objects to an existing array from the given index.

Stack.CopyTo()方法用于将堆栈元素/对象从给定索引复制到现有数组。

Syntax:

句法:

    void Stack.CopyTo(Array, Int32);

Parameters: Array – Targeted array_name in which we have to copy the stack elements/objects, Int32 – is an index in targeted array_name from where stack elements/objects are copied.

参数: Array – 必须在其中复制堆栈元素/对象的目标array_name , Int32 –是目标array_name中从中复制堆栈元素/对象的索引。

Return value: void – it returns nothing.

返回值: void –不返回任何内容。

Example:

例:

    declare and initialize a stack:
    Stack stk = new Stack();
    
    an array declaration for 20 elements:
    int[] arr = new int[20];

    insertting elements:
    stk.Push(100);
    stk.Push(200);
    stk.Push(300);
    stk.Push(400);
    stk.Push(500);

    using CopyTo(), copying stack elements to the array:
    stk.CopyTo(arr, 3); //will copy from 3rd index in array
    
    Output:
    arr: 0 0 0 500 400 300 200 100 0 0 0 0 0 0 0 0 0 0 0 0

使用Stack.CopyTo()方法将堆栈元素/对象复制到数组的C#示例 (C# example to copy stack elements/objects to an array using Stack.CopyTo() method)

using System;
using System.Text;
using System.Collections;

namespace Test
{
    class Program
    {
        //function to print stack elements
        static void printStack(Stack s)
        {
            foreach (Object obj in s)
            {
                Console.Write(obj + " ");
            }
            Console.WriteLine();
        }

        static void Main(string[] args)
        {
            //declare and initialize a stack
            Stack stk = new Stack();

            //an array declaration for 20 elements
            int[] arr = new int[20];
            

            //insertting elements
            stk.Push(100);
            stk.Push(200);
            stk.Push(300);
            stk.Push(400);
            stk.Push(500);

            //printing stack elements
            Console.WriteLine("Stack elements are...");
            printStack(stk);

            //printing array 
            Console.WriteLine("Array elements before CopyTo()...");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            //using CopyTo(), copying stack elements to the array
            stk.CopyTo(arr, 3);

            //printing array 
            Console.WriteLine("Array elements after CopyTo()...");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();            

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

Stack elements are...
500 400 300 200 100
Array elements before CopyTo()...
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Array elements after CopyTo()...
0 0 0 500 400 300 200 100 0 0 0 0 0 0 0 0 0 0 0 0

Reference: Stack.CopyTo(Array, Int32) Method

参考: Stack.CopyTo(Array,Int32)方法

翻译自: https://www.includehelp.com/dot-net/stack-copyto-method-with-example-in-c-sharp.aspx

stack示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值