System.Int32[] c#

  • 本文关键字:Int32 System c#
  • 更新时间 :
  • 英文 :


我正试图编写在数组a和b(AUB)之间建立联合的代码,但当我想打印组合数组时。控制台打印CCD_ 1。

对于上下文,如果我的

array a = { 1, 3, 5, 6 } 

和我的

array b = { 2, 3, 4, 5 } 

我的联合数组应该是

combi = { 1, 2, 3, 4, 5, 6 }

但正如我所说,combi打印为System.Int32[]

我不知道这是我做工会的方式,还是我打印工会的方式。我也在尝试做

  • 交叉点B和A–B

但我还没有尝试过这些,如果你有任何建议,我将不胜感激。

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();
}
}
}

当我想打印组合数组时,控制台会打印System.Int32[]

如果编写Console.Write(someObject),该方法会尝试在someObject中查找被重写的ToString。如果不使用System.Object.ToString,则只返回对象类型的完全限定名称,即"0";System.Int32[]";在CCD_ 8的情况下。

如果你想打印出一个整数数组,一种方法是使用string.Join:

string spaceSeparatedList = string.Join(" ", combi);

现在您有一个以空格分隔的整数列表。

在代码中,您刚刚初始化了Combi array的数组大小没有分配值

首先,您必须将阵列a数组b值添加到组合阵列

然后尝试打印到联合,请参阅下面的代码以获取参考:

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);

相关内容

最新更新