首页 > 其他 > 详细

ArraySegmentSample

时间:2018-01-28 22:25:27      阅读:227      评论:0      收藏:0      [点我收藏+]
    class Program
    {
        static void Main()
        {
            int[] ar1 = { 1, 4, 5, 11, 13, 18 };
            int[] ar2 = { 3, 4, 5, 18, 21, 27, 33 };
            var segments = new ArraySegment<int>[2]
            {
                new ArraySegment<int>(ar1, 0, 3),
                new ArraySegment<int>(ar2, 3, 3)
            };


            var sum = SumOfSegments(segments);
            Console.WriteLine("sum of all segments: {0}", sum);
            Console.ReadKey();

        }

        static int SumOfSegments(ArraySegment<int>[] segments)
        {
            int sum = 0;
            foreach (var segment in segments)
            {
                for (int i = segment.Offset; i < segment.Offset + segment.Count; i++)
                {
                    sum += segment.Array[i];
                }

            }
            return sum;
        }
    }

 

ArraySegmentSample

原文:https://www.cnblogs.com/shy1766IT/p/8372626.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!