Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert a 2D array into 1D array in C#?
Converting a 2D array to a 1D array in C# involves flattening the multi-dimensional structure into a single-dimensional array. This process is commonly used in scenarios like matrix operations, data serialization, or when interfacing with APIs that expect 1D arrays.
Using Nested Loops
The most straightforward approach is using nested loops to iterate through the 2D array and copy elements to the 1D array −
using System;
class Program {
static void Main(string[] args) {
int[,] a = new int[2, 2] {{1, 2}, {3, 4}};
int[] b = new int[4];
int k = 0;
Console.WriteLine("Two-Dimensional Array:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i, j]);
}
}
Console.WriteLine("\nConverting to One-Dimensional Array...");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
b[k++] = a[i, j];
}
}
Console.WriteLine("One-Dimensional Array:");
for (int i = 0; i < b.Length; i++) {
Console.WriteLine("b[{0}] = {1}", i, b[i]);
}
}
}
The output of the above code is −
Two-Dimensional Array: a[0,0] = 1 a[0,1] = 2 a[1,0] = 3 a[1,1] = 4 Converting to One-Dimensional Array... One-Dimensional Array: b[0] = 1 b[1] = 2 b[2] = 3 b[3] = 4
Using LINQ SelectMany Method
LINQ provides a more concise approach using the SelectMany method to flatten arrays −
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
int[,] matrix = new int[3, 3] {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Convert 2D array to jagged array first, then flatten
int[][] jaggedArray = new int[matrix.GetLength(0)][];
for (int i = 0; i < matrix.GetLength(0); i++) {
jaggedArray[i] = new int[matrix.GetLength(1)];
for (int j = 0; j < matrix.GetLength(1); j++) {
jaggedArray[i][j] = matrix[i, j];
}
}
int[] flatArray = jaggedArray.SelectMany(x => x).ToArray();
Console.WriteLine("Original 2D Array:");
for (int i = 0; i < matrix.GetLength(0); i++) {
for (int j = 0; j < matrix.GetLength(1); j++) {
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("\nFlattened 1D Array:");
Console.WriteLine(string.Join(", ", flatArray));
}
}
The output of the above code is −
Original 2D Array: 1 2 3 4 5 6 7 8 9 Flattened 1D Array: 1, 2, 3, 4, 5, 6, 7, 8, 9
Using Generic Method for Any Size Array
Here's a reusable method that works with any size 2D array −
using System;
class Program {
static T[] Flatten2DArray<T>(T[,] array) {
int rows = array.GetLength(0);
int cols = array.GetLength(1);
T[] result = new T[rows * cols];
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[index++] = array[i, j];
}
}
return result;
}
static void Main(string[] args) {
int[,] numbers = new int[2, 4] {
{10, 20, 30, 40},
{50, 60, 70, 80}
};
string[,] words = new string[2, 2] {
{"Hello", "World"},
{"C#", "Programming"}
};
int[] flatNumbers = Flatten2DArray(numbers);
string[] flatWords = Flatten2DArray(words);
Console.WriteLine("Flattened integer array:");
Console.WriteLine("[" + string.Join(", ", flatNumbers) + "]");
Console.WriteLine("\nFlattened string array:");
Console.WriteLine("[" + string.Join(", ", flatWords) + "]");
}
}
The output of the above code is −
Flattened integer array: [10, 20, 30, 40, 50, 60, 70, 80] Flattened string array: [Hello, World, C#, Programming]
Conclusion
Converting a 2D array to a 1D array in C# can be accomplished using nested loops for direct control, LINQ methods for conciseness, or generic methods for reusability. The nested loop approach provides the most straightforward understanding of the flattening process and works efficiently for most scenarios.
