
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to convert a 2D array into 1D array in C#?
Set a two-dimensional array and a one-dimensional array −
int[,] a = new int[2, 2] {{1,2}, {3,4} }; int[] b = new int[4];
To convert 2D to 1D array, set the two dimensional into one-dimensional we declared before −
for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { b[k++] = a[i, j]; } }
The following is the complete code to convert a two-dimensional array to one-dimensional array in C# −
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Program { class twodmatrix { static void Main(string[] args) { int[, ] a = new int[2, 2] { { 1, 2 },{ 3, 4 } }; int i, j; int[] b = new int[4]; int k = 0; Console.WriteLine("Two-Dimensional Array..."); for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i, j]); } } Console.WriteLine("One-Dimensional Array..."); for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { b[k++] = a[i, j]; } } for (i = 0; i < 2 * 2; i++) { Console.WriteLine("{0}\t", b[i]); } Console.ReadKey(); } } }
- Related Articles
- How to convert a tuple into an array in C#?
- How to store a 2d Array in another 2d Array in java?
- How to convert a 2D array to a CSV string in JavaScript?
- Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array
- How to convert an array into a complex array JavaScript?
- Convert 2d tabular data entries into an array of objects in JavaScript
- Turning a 2D array into a sparse array of arrays in JavaScript
- How to dynamically allocate a 2D array in C?
- Matrix product of a 2D (first argument) and a 1D array (second argument) in Numpy
- Matrix product of a 1D (first argument) and a 2D array (second argument) in Numpy
- How to convert an array of characters into a string in C#?
- Python - Convert 1D list to 2D list of variable length
- How to convert a JavaScript array to C# array?
- How to pass a 2D array as a parameter in C?
- Compute the bit-wise AND of a 1D and a 2D array element-wise in Numpy

Advertisements