Iterating One Dimensional Array As Two Dimensional Array-C#

This article will show you how to store a 2D array as a 1D array in C#. (This technique can be applied to any language).

2D

Row: 1 Col: 1 Row: 1 Col: 2 Row: 1 Col: 3
Row: 2 Col: 1 Row: 2 Col: 2 Row: 2 Col: 3
Row: 3 Col: 1 Row: 3 Col: 2 Row: 3 Col: 3

1D
|R1C1| R1C2 | R1C3 | R2C1 | R2C2 | R2C3 | R3C1 | R3C2 | RR3C3 |

Let’s write the program, and then we will understand.

public class _2DArrayTo1D
{

	private int[] _internalArray;
	private int _numberOfColumns;
	public _2DArrayTo1D(int[] arr,int row, int numberOfCols)
	{
		_internalArray = arr;
		_numberOfColumns = numberOfCols;


	}
	public void Set(int row, int col, int value)
	{
		_internalArray[row * _numberOfColumns + col] = value;

	}
	public int Get(int row, int col)
	{
		return _internalArray[row * _numberOfColumns + col];

	}

}



Let’s understand the above code. The main logic is in the Get and Set method. If you want to find the index of the row and col in the one-dimensional array, you have to use the following formula.

1dindex=_arr[row*_numberOfColumns+col];

and then by using the above index, you can get and set the value

$ads={1}

Let’s compare our implementations to a 2d array.

void Main()
{
	var arr2D = new int[,]{

		{1,2,3},
		{4,5,6},
		{7,8,9}

	};

	var arr1D = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	var array = new _2DArrayTo1D(arr1D, 3,3);

	// SET Value
	Console.WriteLine("Set Value");
	arr2D[1, 1] = 10;
	array.Set(1, 1, 10);

	Console.WriteLine("Get Value");
	Console.WriteLine(arr2D[1, 1]);
	Console.WriteLine(array.Get(1, 1));
	
	Console.WriteLine("2D Array");
	Print2D(arr2D);
	Console.WriteLine("1D Array");
	Print1D(array,3,3);




}
public void Print1D(_2DArrayTo1D arr,int row, int numOfCOls)
{


	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < numOfCOls; j++)
		{
			Console.Write(string.Format("{0} ", arr.Get(i, j)));
		}
		Console.Write(Environment.NewLine);
	}
}
public void Print2D(int[,] arr)
{
	int rowLength = arr.GetLength(0);
	int colLength = arr.GetLength(1);

	for (int i = 0; i < rowLength; i++)
	{
		for (int j = 0; j < colLength; j++)
		{
			Console.Write(string.Format("{0} ", arr[i, j]));
		}
		Console.Write(Environment.NewLine );
	}
}

Output

Set Value  
Get Value  
10  
10  
2D Array  
1 2 3  
4 10 6  
7 8 9  
1D Array  
1 2 3  
4 10 6  
7 8 9

Where it is used?

ImageData.data

The readonly ImageData.data property returns a Uint8ClampedArray that contains the ImageData object’s pixel data. Data is stored as a one-dimensional array in the RGBA order, with integer values between 0 and 255 (inclusive).{alertSuccess}

function invertColor(){
	var c = document.getElementById("myCanvas");
	var ctx = c.getContext("2d");
	var img = document.getElementById("scream");
	ctx.drawImage(img, 0, 0);
	var imgData = ctx.getImageData(0, 0, c.width, c.height);

	// invert colors
	var i;
	for (i = 0; i < imgData.data.length; i += 4) {
	  imgData.data[i] = 255-imgData.data[i];
	  imgData.data[i + 1] = 255-imgData.data[i + 1];
	  imgData.data[i + 2] = 255-imgData.data[i + 2];
	  imgData.data[i + 3] = 255;
	}

ctx.putImageData(imgData, 0, 0);
}

Please do not post any spam link in the comment box😊

إرسال تعليق (0)
أحدث أقدم