none
如何获取数组的指定序列 double[] a={5,6,9,7,8.8,7.8,7} 我要获取 后3位 的集合dable[] b={8.8,7.8,7} 如string那样的 substring 可以定义获取数组的指定位数 RRS feed

  • 问题

  • 如题

    如何获取数组的指定序列  double[] a={5,6,9,7,8.8,7.8,7}  我要获取 后3位 的集合dable[] b={8.8,7.8,7}  如string那样的 substring 可以定义获取数组的指定位数

    我看到a.提示哪个是这样的功能? 还是现在没有这样的功能?

    2017年1月5日 6:48

答案

  • Hi,

    据我所知,我没有发现有现成的东西。

    你可以参考下面的方法实现这个功能。

    public static T[] SubArray<T>(this T[] data, int index, int length)
    {
        T[] result = new T[length];
        Array.Copy(data, index, result, 0, length);
        return result;
    }
    static void Main()
    {
        int[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] sub = data.SubArray(3, 4); // contains {3,4,5,6}
    }

    Best  Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 ning050089 2017年1月5日 7:51
    2017年1月5日 7:35
    版主
  • Hi,

    据我所知,我没有发现有现成的东西。

    你可以参考下面的方法实现这个功能。

    public static T[] SubArray<T>(this T[] data, int index, int length)
    {
        T[] result = new T[length];
        Array.Copy(data, index, result, 0, length);
        return result;
    }
    static void Main()
    {
        int[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] sub = data.SubArray(3, 4); // contains {3,4,5,6}
    }

    Best  Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    为了达到我的要求我做了一点更改

     {         int g = 3;
                double[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,6,36,7,324,7,242,7,42,7, };
                double[] sub =SubArray(data,g); // contains {3,4,5,6}
                richTextBox1.Text += string.Join(",", sub);

            }
            public double[] SubArray(double[] data, int index)
            {
                double[] a=new double[index];
                int star = data.Length - index;
              /// 3,3
                Array.Copy(data, star, a, 0, index);
                return a;
            }


    • 已标记为答案 ning050089 2017年1月6日 22:01
    2017年1月6日 22:01

全部回复

  • Hi,

    据我所知,我没有发现有现成的东西。

    你可以参考下面的方法实现这个功能。

    public static T[] SubArray<T>(this T[] data, int index, int length)
    {
        T[] result = new T[length];
        Array.Copy(data, index, result, 0, length);
        return result;
    }
    static void Main()
    {
        int[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] sub = data.SubArray(3, 4); // contains {3,4,5,6}
    }

    Best  Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 ning050089 2017年1月5日 7:51
    2017年1月5日 7:35
    版主
  • Hi,

    据我所知,我没有发现有现成的东西。

    你可以参考下面的方法实现这个功能。

    public static T[] SubArray<T>(this T[] data, int index, int length)
    {
        T[] result = new T[length];
        Array.Copy(data, index, result, 0, length);
        return result;
    }
    static void Main()
    {
        int[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] sub = data.SubArray(3, 4); // contains {3,4,5,6}
    }

    Best  Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    为了达到我的要求我做了一点更改

     {         int g = 3;
                double[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,6,36,7,324,7,242,7,42,7, };
                double[] sub =SubArray(data,g); // contains {3,4,5,6}
                richTextBox1.Text += string.Join(",", sub);

            }
            public double[] SubArray(double[] data, int index)
            {
                double[] a=new double[index];
                int star = data.Length - index;
              /// 3,3
                Array.Copy(data, star, a, 0, index);
                return a;
            }


    • 已标记为答案 ning050089 2017年1月6日 22:01
    2017年1月6日 22:01