How to convert int [] to int [,] - C #

I have an int array in one dimension:

var intArray=new[] { 1, 2, 3, 4, 5, 6 }; 

and I want to convert it to two dimensions, for example:

 var intArray2D=new[,] { {1, 2}, {3, 4}, {5, 6} }; 

How do I achieve this using C #?

+6
source share
8 answers

I believe that you want to split an integer array into an array of two integers:

 int[] list = new int[] { 1, 2, 3, 4, 5, 6}; int[][] newlist = new int[list.Length / 2][]; for (int i = 0, n = 0; i < list.Length; i += 2, n++) { newlist[n] = new[] { list[i], list[i + 1] }; } 

To assign it Points , in particular, you can try:

 List<Point> plist = new List<Point>(); for (int i = 0; i < list.Length; i += 2) { plist.Add(new Point(list[i], list[i + 1])); } 
+1
source

with a loop, perhaps:

 for (int i = 0; i < oldArr.Length; i=i+2) { newArr[i/2, 0] = oldArr[i]; newArr[i/2, 1] = oldArr[i + 1]; } 

Unconfirmed, but should make you point in the right direction ...

+2
source

If a one-dimensional array contains primitive data in the main row order , and the total capacity of a 2-dimensional array is equal to the length of a one-dimensional array, you can use this.

 int[] source = new int[6]; int[,] target = new int[3, 2]; Buffer.BlockCopy(source, 0, target, 0, source.Length * sizeof(int)); 

Note that unlike Array.Copy and other array / list Buffer.BlockCopy works with several bytes of data, even if each element of the array is more than 1 byte. It also only works with arrays of primitive data types.

Additional links:

Edit: Here is the full unit test.

 [TestMethod] public void SOTest16203210() { int[] source = new int[6] { 1, 2, 3, 4, 5, 6 }; int[,] destination = new int[3, 2]; Buffer.BlockCopy(source, 0, destination, 0, source.Length * sizeof(int)); Assert.AreEqual(destination[0, 0], 1); Assert.AreEqual(destination[0, 1], 2); Assert.AreEqual(destination[1, 0], 3); Assert.AreEqual(destination[1, 1], 4); Assert.AreEqual(destination[2, 0], 5); Assert.AreEqual(destination[2, 1], 6); } 
+2
source

you can use this code. here you can send any length you need. you can "deivde" arry [] perform [,] for any lenth.2 or 3 or whatever you like! as long as the size% a.length == 0 !!!!

code

  static int[,] convert(int[] a, int size) { int[,] value = new int[a.Length / size, size]; int counter = 0; // for (int b = 0; b < value.GetLength(0); b++) for (int c = 0; c < value.GetLength(1); c++) { value[b, c] = a[counter]; counter++; } return value; } 
+1
source

If the product size is the same, the fastest way is likely to bind the matrix using a fixed operator, and then use the Marshalling class to copy a continuous block from an array to IntPtr created from void * you received from a fixed one.

You will have to wrap this in an unsafe statement and enable the unsafe assembly configuration of the assembly.

0
source

You can try the following:

 int [] a = {1,2,3,4,5,6}; int [,] b = new int[a.Length/2,2]; for(int i = 0;i<a.Length;i++) { b[i/2,i%2] = a[i]; } 

Note that i / 2 is an integer division, so both 4/2 and 5/2 will give 2, which is the correct index in 2Darray for tuples 5 and 6 from the original array. The second index is determined using a reminder when dividing by 2, it will give 0 if the index is an even number and 1 if the index of the element from the original array is an odd number.

0
source

I converted this from vb to c #

 int Size = OldArray.Length; int[,] NewPoints = null; if (Size % 2 != 0) { //Error - Array has odd number of elements! } else { for (i = 0; i <= Size - 1; i += 2) { Array.Resize(ref AllPoints, (i / 2) + 1, 2); NewPoints[i / 2, 0] = OldArray(i); NewPoints[i / 2, 1] = OldArray(i + 1); } } 
0
source

I would consider this for any array dimension:

 public class TestClass { public static void TestMethod2D() { var intLinear=new[] { 1, 2, 3, 4, 5, 6 }; var indexer2D=new ArrayIndexer<int>(3, 2); for(var i=intLinear.Length; i-->0; indexer2D[i]=intLinear[i]) ; var int2D=(int[,])indexer2D.ToArray(); } public static void TestMethod4D() { var intLinear=new[] { 1, 2, 3, 4, 5, 6 }; var indexer4D=new ArrayIndexer<int>(2, 2, 2, 2); for(var i=intLinear.Length; i-->0; indexer4D[i]=intLinear[i]) ; var int4D=(int[, , ,])indexer4D.ToArray(); } } public partial class ArrayIndexer<T> { public Array ToArray() { return m_Array; } public ArrayIndexer(params int[] lengths) { m_Array=Array.CreateInstance(typeof(T), lengths); } public T this[params int[] indices] { set { m_Array.SetValue(value, indices.Transform(m_Array)); } get { return (T)m_Array.GetValue(indices.Transform(m_Array)); } } Array m_Array; } public static partial class ArrayExtensions { public static int[] Transform( this int[] indices, Array array, bool isRowMajor=true) { if(indices.Length>array.Rank) return indices; else { var list=indices.ToList(); if(isRowMajor) list.Reverse(); for(int r, q=0, i=0, count=array.Rank; count-->0; ++i) { var index=isRowMajor?count-i:i; if(indices.Length>i) { q=Math.DivRem(indices[i]+q, array.GetLength(index), out r); list[i]=r; } else { if(index<0) { list.Add(q); q=0; } else { q=Math.DivRem(q, array.GetLength(index), out r); list.Add(r); } } } if(isRowMajor) list.Reverse(); return list.ToArray(); } } } 

ArrayExtensions.Transform is an index converter; it performs a linear transformation with the geometry of a given array. isRowMajor controls the layout, for example, you isRowMajor int[,] in x, y or in y, x order.

0
source

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


All Articles