Convert Three Dimension Array To One Dimension Array - CSharp System

CSharp examples for System:Array Dimension

Description

Convert Three Dimension Array To One Dimension Array

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//  w w w . j av  a2  s  . c o m

public class Main{
        public static void ConvertThreeDArrayToOneDArray<T>(int xzLen, int yLen, T[, ,] dataIn, T[] dataOut)
        {
            for (int x = 0; x < xzLen; x++)
                for (int y = 0; y < yLen; y++)
                    for (int z = 0; z < xzLen; z++)
                    {
                        dataOut[x * xzLen * yLen + y * xzLen + z] = dataIn[x, y, z];
                    }
        }
        public static T[] ConvertThreeDArrayToOneDArray<T>(int xzLen, int yLen, T[, ,] data)
        {
            T[] result = new T[xzLen * yLen * xzLen];

            for(int x = 0; x < xzLen; x++)
                for(int y = 0; y < yLen; y++)
                    for (int z = 0; z < xzLen; z++)
                    {
                        result[x * xzLen * yLen + y * xzLen + z] = data[x, y, z]; 
                    }

            return result;
        }
}

Related Tutorials