Last active
May 3, 2019 01:24
-
-
Save bbqchickenrobot/42c0c8e252c0d67ed67067c8ad0aaf75 to your computer and use it in GitHub Desktop.
FlattenJaggedArray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| public class Program | |
| { | |
| public static void Main() | |
| { | |
| var jagged = new object[]{ | |
| new object[]{ | |
| 1, 2, new object[]{3} | |
| }, 4 | |
| }; | |
| var flattend = new List<object>(); | |
| void flatten(object obj){ | |
| if(obj.GetType() == typeof(object[])) | |
| { | |
| foreach(var o in (object[])obj) | |
| flatten(o); | |
| }else{ | |
| flattend.Add(obj); | |
| } | |
| } | |
| flatten(jagged); | |
| foreach(var o in flattend) | |
| Console.WriteLine(o); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment