[英]Issues in returning a List<int> from a method and printing values in C#
I have a method like this static List<int> Compare(List<int> a, List<int> b)
I would like this function to return [aPoints bPoints]
something like [2 1]
But, I'm stuck on storing the values after the loop with if
statements and putting them in score.我有一个像这样的方法
static List<int> Compare(List<int> a, List<int> b)
我希望这个 function return [aPoints bPoints]
类似[2 1]
但是,我卡在存储使用if
语句循环后的值并将它们放入分数中。 I tried this:我试过这个:
static List<int> Compare(List<int> a, List<int> b)
{
int aPoints = 0;
int bPoints = 0;
List<int> score = new List<int>() { aPoints, bPoints };
for (int i = 0; i < 3; i++)
{
if (a[i] > b[i])
{
aPoints++;
}
else if (a[i] < b[i])
{
bPoints++;
}
}
return score;
}
And print them on:并将它们打印在:
static void Main(string[] args){}
Here's a very simple solution.这是一个非常简单的解决方案。 Just create an empty list, and add
aPoints
and bPoints
to it right before returning to the caller.只需创建一个空列表,并在返回给调用者之前将
aPoints
和bPoints
添加到其中。
static List<int> Compare(List<int> a, List<int> b)
{
int aPoints = 0;
int bPoints = 0;
List<int> score = new List<int>();
for (int i = 0; i < 3; i++)
{
if (a[i] > b[i])
{
aPoints++;
}
else if (a[i] < b[i])
{
bPoints++;
}
}
score.Add(aPoints);
score.Add(bPoints);
return score;
return
like this:return
时创建您的列表:static List<int> Compare(List<int> a, List<int> b)
{
int aPoints = 0;
int bPoints = 0;
for (int i = 0; i < 3; i++)
{
if (a[i] > b[i])
{
aPoints++;
}
else if (a[i] < b[i])
{
bPoints++;
}
}
return new List<int>() { aPoints, bPoints};
static (int aPoints, int bPoints) Compare(List<int> a, List<int> b)
{
int aPoints = 0;
int bPoints = 0;
for (int i = 0; i < 3; i++)
{
if (a[i] > b[i])
{
aPoints++;
}
else if (a[i] < b[i])
{
bPoints++;
}
}
return (aPoints, bPoints);
}
Following up on your comment, if you want to print the contents of what the Compare
method returns, then, for a List<int>
you can do:跟进您的评论,如果您想打印
Compare
方法返回的内容,那么对于List<int>
您可以执行以下操作:
List<int> ret = Compare(someList, anotherList);
foreach (int n in ret)
{
Console.WriteLine(n);
}
for
loop like this:for
循环:List<int> ret = Compare(someList, anotherList);
for (int i = 0; i < 2; i++)
{
Console.WriteLine(ret[i]);
}
However, in this loop, it's assumed that your list will always have 2 elements inside.但是,在此循环中,假定您的列表中始终包含 2 个元素。 A general approach would be to change the loop definition to
for (int i = 0; i < ret.Count; i++)
so that it iterates over every available item in the list like with the foreach
loop.一种通用的方法是将循环定义更改为
for (int i = 0; i < ret.Count; i++)
以便它像foreach
循环一样遍历列表中的每个可用项目。
Currently you're creating the list before you give aPoints
and bPoints
useful values.目前,您在为
aPoints
和bPoints
有用值之前创建列表。 When you add aPoints
and bPoints
to the list, you're adding the values - so you end up with a list of [0, 0]
.当您将
aPoints
和bPoints
添加到列表中时,您正在添加值- 因此您最终会得到一个[0, 0]
列表。
Instead, you should create the list at the end - and you don't really need a variable for it:相反,您应该在最后创建列表 - 而且您实际上并不需要一个变量:
static List<int> Compare(List<int> a, List<int> b)
{
int aPoints = 0;
int bPoints = 0;
for (int i = 0; i < 3; i++)
{
if (a[i] > b[i])
{
aPoints++;
}
else if (a[i] < b[i])
{
bPoints++;
}
}
return new List<int>() { aPoints, bPoints };
}
A few things to note:需要注意的几点:
a
and b
are.a
和b
有多长,您的代码总是循环 3 次。 While that may be okay now, it's a bit inflexible... any reason you don't want to check they're the same length, and then iterate over them completely?static (int aPoints, int bPoints) Compare(List<int> a, List<int> b)
{
int aPoints = 0;
int bPoints = 0;
for (int i = 0; i < 3; i++)
{
if (a[i] > b[i])
{
aPoints++;
}
else if (a[i] < b[i])
{
bPoints++;
}
}
return (aPoints, bPoints);
}
After all, the list you'll return always has two elements, and I suspect the caller won't want to treat it as a general-purpose list anyway.毕竟,您将返回的列表总是有两个元素,我怀疑调用者无论如何都不想将其视为通用列表。
Printing the results打印结果
If you're still using the list approach for returning the values, your Main method should look something like this:如果您仍在使用列表方法返回值,您的 Main 方法应如下所示:
static void Main()
{
List<int> a = ... ; // However you want to populate this
List<int> b = ... ; // However you want to populate this
List<int> results = Compare(a, b);
Console.WriteLine($"Score for a: {results[0]}");
Console.WriteLine($"Score for b: {results[1]}");
}
If you use the tuple approach, you can be clearer:如果你使用元组的方法,你可以更清楚:
static void Main()
{
List<int> a = ... ; // However you want to populate this
List<int> b = ... ; // However you want to populate this
var results = Compare(a, b);
Console.WriteLine($"Score for a: {results.aPoints}");
Console.WriteLine($"Score for b: {results.bPoints}");
}
Integers are passed by value and not by reference .整数是按值而不是按引用传递的。 This means when you make a
new List<int>() { aPoints, bPoints };
这意味着当你创建一个
new List<int>() { aPoints, bPoints };
, the values of aPoints
and bPoints
, which are both 0, are copied into the list. ,
aPoints
和bPoints
的值都为 0,被复制到列表中。 Changing their values afterwards won't affect the copies stored in the list.之后更改它们的值不会影响存储在列表中的副本。
For this specific situation, the easiest thing would be to simply move your line List<int> score = new List<int>() { aPoints, bPoints };
对于这种特定情况,最简单的方法是简单地移动您的行
List<int> score = new List<int>() { aPoints, bPoints };
after your loop.在你的循环之后。 Then, the points values will be copied over after they've been set correctly.
然后,点值将在正确设置后被复制。
aPoints and bPoints are value types, not reference types, so put them in the end: aPoints 和 bPoints 是值类型,不是引用类型,所以把它们放在最后:
static List<int> Compare(List<int> a, List<int> b)
{
int aPoints = 0;
int bPoints = 0;
for (int i = 0; i < 3; i++)
{
if (a[i] > b[i])
{
aPoints++;
}
else if (a[i] < b[i])
{
bPoints++;
}
}
return new List<int>() { aPoints, bPoints };
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.