I'm trying to write code that makes a union between array a and b (AUB), but when I want to print the combined array. the console prints System.Int32\[\]
.
For context, if my
array a = { 1, 3, 5, 6 }
and my
array b = { 2, 3, 4, 5 }
my union array should be
combi = { 1, 2, 3, 4, 5, 6 }
but like I said, combi
prints out as System.Int32\[\]
I don't know if it's the way I'm doing the union or if its the way I'm printing it. I'm also trying to do a
- A intersection B and A – B
But I haven't tried anything of those yet, I would appreciate if you have any tips.
using System;
using System.Reflection.Metadata.Ecma335;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("size of A? ");
int a = Convert.ToInt32(Console.In.ReadLine());
Console.WriteLine("Value of A:");
int[] ans = new int[a];
for (int i = 0; i < ans.Length; i )
{
ans[i] = Convert.ToInt32(Console.In.ReadLine());
}
Console.WriteLine("size of B?");
int b = Convert.ToInt32(Console.In.ReadLine());
Console.WriteLine("Value of B:");
int[] anse = new int[b];
for (int i = 0; i < anse.Length; i )
{
anse[i] = Convert.ToInt32(Console.In.ReadLine());
}
var combi = new int[ans.Length anse.Length];
for (int i = 0; i < combi.Length; i )
{
Console.WriteLine(combi " ");
}
Console.ReadLine();
}
}
}
CodePudding user response:
when i want to print the combined array the console prints System.Int32[]
If you write Console.Write(someObject)
, the method tries to find an overridden ToString
in someObject
. If there is none System.Object.ToString
is used which just returns the fully qualified name of the type of the Object, which is "System.Int32[]" in case of an int[]
.
If you want to print out an array of integers one approach would be to use string.Join
:
string spaceSeparatedList = string.Join(" ", combi);
Now you have a space-separated list of integers.
CodePudding user response:
In the code You have just initialized the size of the array of Combi Array not assign the values
First you have to add Array a and Array b values into combi array
Then try to print into union,see below code for the reference:
List<int> combi = new List<int>();
foreach(var first in ans)
{
combi.Add(first);
}
foreach (var second in anse)
{
combi.Add(second);
}
combi.Sort();
string result = string.Join(",",combi);
Console.WriteLine(result);