Array definition: An array is a data structure that can store multiple values of the same type in a contiguous block of memory.
Arrays can have one or more dimensions, depending on how many indices are needed to access an element. A one-dimensional (1D) array is often referred to as a vector, while a two-dimensional (2D) array is commonly known as a matrix.
In this article, we will discuss some of the common methods to convert a 2D array into a 1D array using C#, Python, Java, and C programs with code examples.
Table of Contents
Convert 2d array to a 1d array in C#
Here is an example of using a nested loop to convert a 2D array into a 1D array in C# in row-major order:
using System;
class Program
{
static void Main()
{
// Creating a 2d Array with 2 rows and three columns
int[,] twoDArray = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int rows = twoDArray.GetLength(0); // Get the number of rows
int cols = twoDArray.GetLength(1); // Get the number of columns
// Creating a 1D array with enough space
int[] oneDArray = new int[rows * cols];
int index = 0;
for (int i = 0; i < rows; i++) // Loop over the rows of the 2D array
{
for (int j = 0; j < cols; j++) // Loop over the columns of the 2D
{
// Copy the element from the 2D array to the 1D array
oneDArray[index++] = twoDArray[i, j];
}
}
// Display the 1D array
Console.WriteLine(string.Join(", ", oneDArray));
}
}
The resulting array will look like this:
1, 2, 3, 4, 5, 6
Code Explanation:Â
In this C# example, we define the dimensions of the 2D array. Subsequently, we create a 1D array with the correct size. During this process, each element is transferred to the corresponding position in the newly created 1D array from the 2D array.
Ultimately, we iterate through the elements of the 1D array to display the result.
Convert 2d array to 1d array in python
In Python, we can also use a list comprehension to iterate over the 2D array and append its elements into a 1D array. We can use the for
 keyword to loop over the rows and columns of the 2D array, and use the in
 keyword to access the elements.
For example, if we want to convert the 2D array in row-major order, we can use this list comprehension:
two_d_array = [[1, 2, 3], [4, 5, 6]]
one_d_array = [element for row in two_d_array for element in row]
# Display the 1D array
print(one_d_array)
Result:
[1, 2, 3, 4, 5, 6]
Convert 2d array to a 1d array in Java
This Java example follows a similar approach to C# to convert a 2D array into a 1D array. We are iterating through the 2D array and populating the 1D array accordingly.
import java.util.Arrays;
public class ArrayConversion {
public static void main(String[] args) {
int[][] twoDArray = { { 1, 2, 3 }, { 4, 5, 6 } };
int rows = twoDArray.length;
int cols = twoDArray[0].length;
int[] oneDArray = new int[rows * cols];
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
oneDArray[index++] = twoDArray[i][j];
}
}
// Display the 1D array
System.out.println(Arrays.toString(oneDArray));
}
}
Output:
[1, 2, 3, 4, 5, 6]
Convert 2d array to a 1d array in C
In C, we manually calculate the number of rows and columns, then iterate through the 2D array to fill the 1D array.
#include <stdio.h>
int main() {
int twoDArray[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
int rows = sizeof(twoDArray) / sizeof(twoDArray[0]);
int cols = sizeof(twoDArray[0]) / sizeof(twoDArray[0][0]);
int oneDArray[rows * cols];
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
oneDArray[index++] = twoDArray[i][j];
}
}
// Display the 1D array
for (int i = 0; i < rows * cols; i++) {
printf("%d ", oneDArray[i]);
}
return 0;
}
Output:
1 2 3 4 5 6
Recommended Articles:
- How to remove duplicate characters from a String in C#
- C# program to count the occurrences of each character in a String
- C# program to count vowels and consonants in a string
- Leap Year Program in C, C++, C#, JAVA, PYTHON, and PHP
- Program to print prime numbers from 1 to N
- C# Program to Check if a Given Number is Even or Odd
- Bubble sort programs in C, C++, JAVA, and PYTHON
- Fibonacci sequence: Fibonacci series in C# (with examples)
- Program to copy all elements of an array into another array
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- C# Programs asked in Interviews
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024