C# - 将包含给定数组中所有元素的列表作为字符串返回 [英] C# - return a list containing all the elements in the given array as strings

查看:144
本文介绍了C# - 将包含给定数组中所有元素的列表作为字符串返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试这个问题 - 返回一个List,其中包含给定数组中的所有元素作为字符串,前面是字符串element X : 。 。 ,其中x =元素的索引。我总觉得我接近正确的代码,但数组,列表和集合的概念仍然是我逐渐习惯的。关于标题中的问题,我试图用for循环来回答它,但是花括号里面会有什么?



我一直得到像这样的错误如果你知道我的意思,这种类型的东西不能转换成这种类型的东西。我想了解如何克服这个障碍。



亲切的问候



我尝试过:



Hi,

I am attempting this question - Return a List containing all the elements in the given array as strings, prepended by the string "element x : ". . , where x = the index of the element. I always feel as if I am close to the right code, but the concept of arrays, lists and collections is something I am still gradually becoming accustomed to. Regarding the question in the title, I have attempted to answer it using a for loop, but what would go inside the curly braces?

I keep getting errors like " "this type of thing" cannot be converted to "this type of thing" ", if you know what I mean. I'd like to understand how to get around that hurdle.

Kind regards

What I have tried:

public static List<string> RetrunModifiedList(DateTime[] anArray)
       {
           for (int i = 0; i < anArray.Length; i++)
               {
             anArray[i] = i.ToString().Prepend("element" i);
               }
           return anArray;

       }

推荐答案

您已声明输入参数 anArray 作为包含DateTime对象的数组 - 但在循环内部,您尝试用字符串填充它。你不能这样做:就像有一个圆孔框架,并试图将方形钉钉入其中!



要返回字符串,你必须创建一个新的包含字符串的集合,并将DateTime对象 - 作为字符串 - 传输到它。

我这样做的方式 - 至少作为初学者 - 将是这样的:

You have declared the input parameter anArray as being an array containing DateTime objects - but inside the loop you attempt to fill it with strings. You can't do that: that's like having a frame of round holes, and trying to fit square pegs into them!

To return strings, you must create a new collection that holds strings, and transfer the DateTime objects - as strings - into it.
The way I would do it - at least as a beginner - would be like this:
public static List<string> RetrunModifiedList(DateTime[] anArray)
   {
   List<string> results = new List<string>();
   for (int i = 0; i < anArray.Length; i++)
      {
      results.Add(string.Format("Element {0} : {1}", i, anArray[i]));
      }
   return results;
   }






看到代码片段后,我可以看到你正在将DateTime []更新为字符串数组。



你能否为此声明单独的变量。



eg.g



List< string> listArray = new List< string>();



和for循环内部



listArray.Add( i.ToString()。Prepend(elementi));



并返回listArray对象。



试试这个。





Hi,

After seeing the code snippet, I can see you are updating DateTime[] to string array.

Could you please declare separate variable for this.

eg.g

List<string> listArray= new List<string>();

And inside for loop

listArray.Add(i.ToString().Prepend("element" i));

and return listArray object.

Try this.


public static List<string> RetrunModifiedList(DateTime[] anArray)
       {
             List<string> listArray= new List<string>();

           for (int i = 0; i < anArray.Length; i++)
               {
            listArray.Add(i.ToString().Prepend("element" i));
               }
           return listArray;

       }


这篇关于C# - 将包含给定数组中所有元素的列表作为字符串返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆