Click here to Skip to main content
Click here to Skip to main content

Tagged as

Translating a jagged array

, 13 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Code to translate a jagged array.

The code below translates a jagged array. For a jagged array A, the result is a n*m jagged array, where the outer size is n=max(A[i].Length) and the inner size of each subarray is m=A.GetLength(0).

Remark: Holes after null or incomplete subarrays will be filled with default(T).

Usage:

MyType[][] array = ...;
MyType[][] translated = array.Translate();

For examle:

  1. array = new int[5][] { 
        new int[]  { 5, 6, 7    },
        new int [] { 1, 2, 3, 4 },
        new int [] {            },
        new int [] { 0, 1       },
        null,
    };
    expected = new int[4][] { 
        new int[] { 5, 1, 0, 0, 0 },
        new int[] { 6, 2, 0, 1, 0 },
        new int[] { 7, 3, 0, 0, 0 },
        new int[] { 0, 4, 0, 0, 0 },
    };
  2. array = new int[2][] { 
        null,
        null,
    };
    expected = new int[1][] { 
        new int[] { 0, 0 },
    };

    For nullable types, there would be null instead of 0.

    public static class JaggedArrayExtensions
    { 
        /// <summary>
        /// Translates a given jagged array. Holes after null
        /// or incomplete subarrays will be fileld with default(T).
        /// </summary>
        /// <typeparam name="T">Type of elements in the array</typeparam>
        /// <param name="array">The array to translate</param>
        /// <returns>A n*m jagged array, where the outer size is n=max(A[i].Length) and 
        /// the inner size of each subarray is m=A.GetLength(0).</returns>
        public static T[][] Translate<t>(this T[][] array)
        {
            if (array == null)
                throw new ArgumentNullException();
            if (array.Length == 0)
                return new T[0][];
            int columnCount = array.Max(sub => sub == null ? 1 : sub.Length);
            int rowCount = array.Length;
            T[][] res = new T[columnCount][];
            for (int i = 0; i < columnCount; i++)
            {
                res[i] = new T[rowCount];
                for (int j = 0; j < rowCount; j++)
                {
                    if (array[j] == null || i >= array[j].Length)
                        res[i][j] = default(T);
                    else
                        res[i][j] = array[j][i];
                }
            }
            return res;
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jacek Gajek
Software Developer (Junior)
Poland Poland
My name is Jacek Gajek. I have graduated in computer science with a master's degree from Polibuda in Wrocław. I like C# and Monthy Python's sense of humour.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 13 Dec 2011
Article Copyright 2011 by Jacek Gajek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid