How to get a full row or column from a 2D array in C #

I don't want to use a jagged array, I have a two-dimensional array, and I want to get a full column or row without going through them. Does anyone have any idea how to do this.

double [,] array = new double [3,3] ; 1 2 3 4 5 6 Out: 1 2 3 or 2 5 
+23
source share
8 answers

You can optimize it to get the rows using Buffer.BlockCopy() , but in order to get the column, you have to loop. Buffer.BlockCopy() ultimately uses a processor instruction to copy a block of memory, so it is pretty fast.

It’s convenient to put the code in an extension method so that it is easier to call. Please note that Buffer.BlockCopy() can only be used for arrays of primitive types, i.e. int , double , char , etc. This does NOT include string .

Here is a compiled example:

 using System; using System.Linq; using System.Runtime.InteropServices; namespace ConsoleApplication4 { public static class Program { private static void Main() { var array = new [,] { {0.1, 0.2, 0.3, 0.4, 0.5}, {1.1, 1.2, 1.3, 1.4, 1.5}, {2.1, 2.2, 2.3, 2.4, 2.5}, {3.1, 3.2, 3.3, 3.4, 3.5}, }; var row = array.GetRow(2); // This prints 2.1, 2.2, 2.3, 2.4, 2.5 Console.WriteLine(string.Join(", ", row.Select(element => element.ToString()))); } } public static class ArrayExt { public static T[] GetRow<T>(this T[,] array, int row) { if (!typeof(T).IsPrimitive) throw new InvalidOperationException("Not supported for managed types."); if (array == null) throw new ArgumentNullException("array"); int cols = array.GetUpperBound(1) + 1; T[] result = new T[cols]; int size; if (typeof(T) == typeof(bool)) size = 1; else if (typeof(T) == typeof(char)) size = 2; else size = Marshal.SizeOf<T>(); Buffer.BlockCopy(array, row*cols*size, result, 0, cols*size); return result; } } } 
+11
source

To get a specific row or column from a multidimensional array, you can use LINQ:

 public class CustomArray<T> { public T[] GetColumn(T[,] matrix, int columnNumber) { return Enumerable.Range(0, matrix.GetLength(0)) .Select(x => matrix[x, columnNumber]) .ToArray(); } public T[] GetRow(T[,] matrix, int rowNumber) { return Enumerable.Range(0, matrix.GetLength(1)) .Select(x => matrix[rowNumber, x]) .ToArray(); } } 
+23
source

I don’t think there is a way without a loop. It can be done with LINQ, but it forces you to do it.

+3
source

There are no miracles in computers.
Is this what you want?

 class ColumnAccessor<T> where T : class { int columnIndex; T[,] arr; public ColumnAccessor(T[,] arr, int columnIndex) { this.columnIndex = columnIndex; this.arr = arr; } public T Get(int rowIndex) { return this.arr[columnIndex, rowIndex]; } } 
+1
source

Here is how I did it, you can use

GetLength (0)

to get columns and use

GetLength (1)

to get the rows of a 2-dimensional array, and you iterate through the for loop if someone else needs it.

 string text = ""; for (int i = 0; i < array.GetLength(0); i++) { text += Convert.ToString(array[i, 2]) + "\n"; } 
+1
source

an alternative way you can do this is to use a List instead of an array .

In particular, in your case, you would do something like this:

  • First create an inner class representing an array of tuples
  • Create an Inner Class List
  • Fill Inner Class
  • Get a string containing something specific
  • Get a column containing something specific
 public static void Main(string[] args) { // #2 -- Instantiate List of myClass List<myClass> myList = new List<myClass>(); // // #3 -- Populate the list myList.Add(new myClass(1,2,3)); myList.Add(new myClass(3,4,5)); myList.Add(new myClass(5,6,6)); // // #4 -- Get the line where a == 1 myList.Find(x=>xa == 1); // // #5 -- Get column b myList.Select(x=>xb); } 
 // #1 -- Create the inner class public class myClass { public int a; public int b; public int c; public myClass(int a, int b, int c) { this.a =a; this.b =b; this.c =c; } } 
0
source

what you need is a Jugged array (not a multi-dimensional array)

https://msdn.microsoft.com/en-us/library/2s05feca.aspx

 int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[5]; jaggedArray[1] = new int[] { 0, 2, 4, 6 }; jaggedArray[2] = new int[] { 11, 22 }; 

full example with columns:

 using System; using System.Collections.Generic; namespace Rextester { public class Program { public static T[] column<T>(T[][] jaggedArray,int wanted_column) { T[] columnArray = new T[jaggedArray.Length]; T[] rowArray; for(int i=0;i<jaggedArray.Length;i++) { rowArray=jaggedArray[i]; if(wanted_column<rowArray.Length) columnArray[i]=rowArray[wanted_column]; } return columnArray; } public static void Main(string[] args) { //Your code goes here int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[5]; jaggedArray[1] = new int[] { 0, 2, 4, 6 }; jaggedArray[2] = new int[] { 11, 22 }; Console.WriteLine("Hello, world!"); Console.WriteLine(string.Join(" ",jaggedArray[1])); Console.WriteLine(string.Join(" ",column(jaggedArray,1))); } } } 

another same idea using extensions

 using System; using System.Collections.Generic; namespace Rextester { public static class MyExtensions { public static string Extend(this Array array) { return "Yes, you can extend an array"; } public static T[] column<T>(this T[,] multidimArray,int wanted_column) { int l=multidimArray.GetLength(0); T[] columnArray = new T[l]; for(int i=0;i<l;i++) { columnArray[i]=multidimArray[i,wanted_column]; } return columnArray; } public static T[] row<T>(this T[,] multidimArray,int wanted_row) { int l=multidimArray.GetLength(1); T[] rowArray = new T[l]; for(int i=0;i<l;i++) { rowArray[i]=multidimArray[wanted_row,i]; } return rowArray; } } public class Program { public static void Main(string[] args) { Console.WriteLine("Hello, world!"); int [,] multidimArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; Console.WriteLine(string.Join(" ",multidimArray.column(0))); Console.WriteLine(string.Join(" ",multidimArray.row(0))); } } } 
0
source

if you know the index of numbers for output ... then you do not need to use a loop to get the desired result ...

 double[,] array = new double[3,3] {{1,2,3}, {4,5,6}, {7,8,9}}; int firstNum = array[0,1]; int secondNum = array[1,1]; 

get 2, 5

-4
source

Source: https://habr.com/ru/post/1208827/


All Articles