How to access and re-format Jagged Array in C# - c#

I have 2D array in c#, like this:
int[][] 2darray = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
how can I get one column as normal array, like
int[] array = 2darray[1][]; //example, not working
and have
int[] array = {3,4};
?
Thanks.

There are several reasons why your code can't compile
This way it works:
int[][] array2d = { new[]{ 1, 2 }, new[]{ 3, 4 }, new[]{ 5, 6 }, new[]{ 7, 8 } };
int[] array = array2d[0];
Problems:
2darray is not a valid variable name
The indexing is wrong
The initialization of the original array is wrong
EDIT:
As stated by #heltonbiker, if you require all elements of the first column, you can use this:
int[] col = array2d.Select(row => row[0]).ToArray();

For an array with two columns and four rows, you can use LINQ this way:
using System.Linq;
first_column = _2darray.Select(row => row[0]).ToArray();
Note that changing the first or second array will not change the other one.

You are confusing jagged arrays and multidimensional arrays in C#. While they are similar, there is a slight difference. Rows in a jagged array can have a different number of elements, while in a 2D-array they are of the same length. Therefore when working with jagged arrays you need to remember to write handling for a missing column element. I composed a sample console app below to show how both of them work - it uses 0 as a substitute for a missing element, but you can throw an error etc.:
using System.Collections.Generic;
namespace JaggedArrayExample
{
class Program
{
static void Main(string[] args)
{
//jagged array declaration
int[][] array1;
//jagged array declaration and assignment
var array2 = new int[][] {
new int[] { 1, 2 },
new int[] { 3, 4 },
new int[] { 5, 6 },
new int[] { 7, 8 }
};
//2D-array declaration
int[,] array3;
//2D-array declaration and assignment (implicit bounds)
var array4 = new int[,] {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
//2D-array declaration and assignment (explicit bounds)
var array5 = new int[4, 2] {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
//get rows and columns at index
var r = GetRow(array2, 1); //second row {3,4}
var c = GetColumn(array2, 1); //second column {2,4,6,8}
}
private static int[] GetRow(int[][] array, int index)
{
return array[index]; //retrieving the row is simple
}
private static int[] GetColumn(int[][] array, int index)
{
//but things get more interesting with columns
//especially if jagged arrays are involved
var retValue = new List<int>();
foreach (int[] r in array)
{
int ub = r.GetUpperBound(0);
if (ub >= index) //index within bounds
{
retValue.Add(r[index]);
}
else //index outside of bounds
{
retValue.Add(0); //default value?
//or you can throw an error
}
}
return retValue.ToArray();
}
}
}

try this, it should work
int[] array = array2d[1];
Change the name of the variable to array2d, you cannot have variable that starts with number, a variable can start with letter or underscore.

Related

Мultidimensional Аrrays as an argument for method

I wrote a method, that takes a multidimensional array as a parameter and returns the largest numbers of each:
static double[] FindLargest( double[][] NumsInNums ) {
double[] Larges = new double[] {};
int i = 0;
foreach( double[] Nums in NumsInNums ) {
Larges[i] = Nums.Max();
i++;
}
return Larges;
}
But when i call it:
static void Main(string[] args)
{
double[] nums = FindLargest( {{4, 2, 7, 1}, {20, 70, 40, 90}, {1, 2, 0}} );
foreach(double num in nums) {
Console.WriteLine(num);
}
}
But this error appers:
What is wrong here?
You have two problems. Firstly your array declaration has an invalid syntax. It should be like this:
var nums = FindLargest(
new []
{
new double[] { 4, 2, 7, 1},
new double[] { 20, 70, 40, 90},
new double[] { 1, 2, 0}
});
(I think that was probably OK in your actual code, because you seem to be asking about a different problem; see below!)
Secondly, in your FindLargest() method you are creating your result array, Larges with a size of zero. You need to create it with the correct size to accommodate all the results - in this case, it must be the same size as the NumsInNums array, which you can find via NumInNums.Length:
static double[] FindLargest(double[][] NumsInNums)
{
double[] Larges = new double[NumsInNums.Length];
int i = 0;
foreach (double[] Nums in NumsInNums)
{
Larges[i] = Nums.Max();
i++;
}
return Larges;
}
Just for completeness, I should point out that you can use the Linq Enumerable.Select() to simplify the code like so:
static double[] FindLargest(double[][] NumsInNums)
{
return NumsInNums.Select(array => array.Max()).ToArray();
}
The .Select() takes each element of NumInNums (each element is an array) and then calls Enumerable.Max() for it, then takes all the results and puts them in an array (via the ToArray()).

How create multidimensional array in c#

I would like to create a multidimensional array in c#, but I`m not sure how. The array should look like this:
array1, array2, array3
array4, array5, array6
array7, array8, array9
The each small array will store 3 ints. I managed to create a multidimensional array that stores 1 array on each line, but I need to store 3 arrays on each line.
The code is below:
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 3, 5 };
jaggedArray[1] = new int[] { 0, 2, 4 };
jaggedArray[2] = new int[] { 11, 22, 33 };
Your description corresponds to 2d array of arrays int[,][]:
int[] array1 = new int[] {1, 2, 3};
...
int[] array9 = new int[] {89, 562, 356};
...
// 2d array of arrays (array1..array9)
int[,][] array = new int[,][] {
{ array1, array2, array3, },
{ array4, array5, array6, },
{ array7, array8, array9, },
};
You can declare as given below. you can use jagged arrays.
int[][,] jaggedArray4 = new int[3][,]
{
new int[,] { {1,3,3}, {5,7,7},{1,2,3} },
new int[,] { {1,3,3}, {5,7,7},{1,2,3} },
new int[,] { {1,3,3}, {5,7,7},{1,2,3} }
};
Console.WriteLine(jaggedArray4[0][1,0]);//displayed first row , 2nd element array's first element
If you always have 3x3x3, you can use a 3d array:
int[,,] my3dArray = new int[3,3,3];
And you can access it like so:
my3dArray[0,0,0] = 5;
my3dArray[0,0,1] = 2;
my3dArray[1,2,0] = 41;
Console.WriteLine(myArray[1,2,0]); // prints 41
If each dimension is a variable size, you could used an array of arrays of arrays (kind of a 3d jagged array):
int[][][] myJagged = new int[3][][];
myJagged[0] = new int[3][];
myJagged[0][0] = new int[] {1, 2, 3}; // etc. until you've initialized all the arrays at all levels
Console.WriteLine(myJagged[0][0][2]); // prints 3
Alternatively, if only your last dimension is a variable size (or if it's easier to work with), you could use a 2d array of arrays:
int[,][] myArray = new int[3,3][];
myArray[0,0] = new int[] { 5, 2, 41 };
Console.WriteLine(myArray[0,0][2]); // prints 41

Trying to arrange a 2D array in ascending order C#

I'm trying to arrange a 2D array in ascending order. This code runs for a 1D array but i'm struggling to implement it for 2D so that it places each row of the array in ascending order.
1D
public static void Main(string[] args)
{
int[] sam = { 4, 7, 2, 0 };
Array.Sort(sam);
foreach (int value in sam)
{
Console.Write(value);
Console.Write(' ');
}
Console.WriteLine();
}
2D
public static void Main(string[] args)
{
int[] sam = { 4, 7, 2, 0 }, {4, 6, 2, 0};
Array.Sort(sam);
foreach (int value in sam)
{
Console.Write(value);
Console.Write(' ');
}
Console.WriteLine();
}
You can do something like this:
static void Main(string[] args) {
int[][] sam = new int[2][];
sam[0] = new int[] {4, 6, 2, 0};
sam[1] = new int[] {4, 7, 2, 0};
foreach(var array in sam)
{
Array.Sort(array);
foreach(var item in array)
Console.Write(item+" ");
Console.WriteLine();
}
}
Here, we are declaring a 2D array and then initializing each array one at a time. Then, we are looping through the sam array and then sorting each 1D array within it.
There are multiple solutions, one solution I'm thinking is using Array of Arrays (Jagged Array).
Let's say, if you have jagged array as defined below
int[][] numbers2 = new int[][] {
new int[]{4,5,6},
new int[]{1,2,3},
new int[]{7,8,9}
...
};
You can get sorted Jagged array using
int[][] sorted = numbers2.Select(x=> string.Join("-", x.ToArray()))
.OrderBy(x=>x)
.Select(x=> x.Split('-').Select(int.Parse).ToArray())
.ToArray();
Another option, let's your input is a 2d array you could do more or less same but output is sorted Jagged array.
int[][] sorted_array = numbers.Cast<int>()
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / (numbers.GetUpperBound(0) +1))
.Select(x => string.Join("-", x.Select(v => v.Value)) )
.OrderBy(x=>x)
.Select(x=> x.Split('-').Select(int.Parse).ToArray())
.ToArray();
These are just my options, you will be the best to choose which is right to your solution.
Check this Example

Merging arrays with common element

I want to merge arrays with common element. I have list of arrays like this:
List<int[]> arrList = new List<int[]>
{
new int[] { 1, 2 },
new int[] { 3, 4, 5 },
new int[] { 2, 7 },
new int[] { 8, 9 },
new int[] { 10, 11, 12 },
new int[] { 3, 9, 13 }
};
and I would like to merge these arrays like this:
List<int[]> arrList2 = new List<int[]>
{
new int[] { 1, 2, 7 },
new int[] { 10, 11, 12 },
new int[] { 3, 4, 5, 8, 9, 13 } //order of elements doesn't matter
};
How to do it?
Let each number be a vertex in the labelled graph. For each array connect vertices pointed by the numbers in the given array. E.g. given array (1, 5, 3) create two edges (1, 5) and (5, 3). Then find all the connected components in the graph (see: http://en.wikipedia.org/wiki/Connected_component_(graph_theory))
I'm pretty sure it is not the best and the fastest solution, but works.
static List<List<int>> Merge(List<List<int>> source)
{
var merged = 0;
do
{
merged = 0;
var results = new List<List<int>>();
foreach (var l in source)
{
var i = results.FirstOrDefault(x => x.Intersect(l).Any());
if (i != null)
{
i.AddRange(l);
merged++;
}
else
{
results.Add(l.ToList());
}
}
source = results.Select(x => x.Distinct().ToList()).ToList();
}
while (merged > 0);
return source;
}
I've used List<List<int>> instead of List<int[]> to get AddRange method available.
Usage:
var results = Merge(arrList.Select(x => x.ToList()).ToList());
// to get List<int[]> instead of List<List<int>>
var array = results.Select(x => x.ToArray()).ToList();
Use Disjoint-Set Forest data structure. The data structure supports three operations:
MakeSet(item) - creates a new set with a single item
Find(item) - Given an item, look up a set.
Union(item1, item2) - Given two items, connects together the sets to which they belong.
You can go through each array, and call Union on its first element and each element that you find after it. Once you are done with all arrays in the list, you will be able to retrieve the individual sets by going through all the numbers again, and calling Find(item) on them. Numbers the Find on which produce the same set should be put into the same array.
This approach finishes the merge in O(α(n)) amortized (α grows very slowly, so for all practical purposes it can be considered a small constant).

Converting array/matrix initialization code from C++ to C#

I am in the middle of converting C++ code to C#. I am having trouble with some C++ array static initialization. Consider the following piece of C++:
int Values[][32] = {
{}, {}, {1, 2, 3}, {1}
};
What this does is creates a matrix of 4x32 integers. I need to do same or similar in C# in as straightforward way as possible. I am looking to get either
static int[][] Values = {...};
...or
static int[,] Values = {...};
The thing is C# does not seem to allow array initialization with uneven sub-array sizes. Other than this, if one specifies new int[4,32] {...} then everything between curly braces must be of exactly 32 length. With C++ one can specify {1, 2, 3} and the compiler will fill in the rest of the missing array members with zeros.
[EDIT] I have tried using LINQ and got what I desire but it looks cumbersome.
In C# the size of your array initializer must match the declared size of the array. You do not need to declare the size if you would like to avoid duplication.
If you need all entries to be of the same size, you can do it with a little helper function Expand, like this:
static int[] Expand(int[] src, int size) {
var res = new int[size];
Array.Copy(src, res, src.Length);
return res;
}
static int[][] Values = new int[4][] {
Expand(new[] {1}, 32)
, Expand(new[] {1,2,3}, 32)
, Expand(new[] {1,2,3}, 32)
, Expand(new[] {1,2,3, 4, 5}, 32)
};
I deleted my previous answer and am adding another (less eloquent) one that should work.
// utility function you can put in a class
static int[] BuildArray32(params int[] values)
{
int[] retVal = new int[32];
Array.Copy(values, retVal, values.Length);
return retVal;
}
// array initializer code
int[][] Values = {
BuildArray32(),
BuildArray32(),
BuildArray32(1, 2, 3),
BuildArray32(1),
};
EDIT Or you could make a builder class :)
class MatrixBuilder
{
int width;
List<int[]> rows;
public MatrixBuilder(int width)
{
this.width = width;
this.rows = new List<int[]>();
}
public MatrixBuilder Add(params int[] row)
{
int[] wideRow = new int[width];
Array.Copy(row, wideRow, row.Length);
rows.Add(wideRow);
return this;
}
public int[][] ToMatrix()
{
return rows.ToArray();
}
}
int[][] Values2 = new MatrixBuilder(32)
.Add()
.Add()
.Add(1, 2, 3)
.Add(1)
.ToMatrix();
You can't do it in c#. you can either initialise the arrays all to zeros, then apply your known initial values afterwards:
int[][] initAll = new []
{
new int[32] ,
new int[32] ,
new int[32] ,
new int[32]
};
or initialize a jagged array:
int[][] initJagged = new[]
{
new[] {1, 3, 5, 7, 9},
new[] {1, 3, },
new[] {1, 3,0,0,0,0,},
new int[32]
};

Categories