问题描述
我有一个数组
ArrayList array = new ArrayList();
array.Add("a");
array.Add("b");
array.Add("c");
,我有一个字符串变量 refFormat ,其格式如下。
and I have a string variable refFormat which has the format as below.
string refFormat = "{2} {0}";
我正在尝试使用这种格式从数组中获取一串值。下面是我写的。
I'm trying to get a string of values from the array with this format. Below is what I have written.
string newStr = String.Format(refFormat,array.ToArray());
在尝试执行此操作时遇到以下异常。
I'm getting the following exception when I'm trying to do this.
我知道这个问题听起来很重复,但是我的疑问是如何从数组中选取值,该值的索引是以 2 格式指定的, 0 。请帮忙。
I know this question sounds repeated but my doubt is how to pick the values from the array whose indexes are the ones specified in the format that 2 and 0. Please help..
编辑:抱歉,您提出了错误的问题。我正在尝试使用arraylist而不是string数组。尽管使用ToArray()将其转换为数组,我仍然遇到异常。我要去哪里错了?而且我不能在这里使用List而不是arraylist,因为数组包含不同类型的数据。请帮助我。.
Edit: Hi sorry for putting up the wrong question. I'm using an arraylist instead of a string array I'm trying the same. I'm still getting the exception in spite of converting it to an array using ToArray(). Where am I going wrong? And also I cannot use List instead of arraylist here since the array contains data of different type. Please help me out..
推荐答案
作为参考,这也是我的工作代码:
For reference, here is my working code too:
string[] array = new string[] { "a", "b", "c", "d" };
string refFormat = "{2} {0}";
string newStr = String.Format(refFormat, array);
Console.WriteLine(newStr);
运行上面的代码时我没有遇到错误。
I did not encounter an error when running the above code.
这篇关于使用String.Format时的异常“索引(从零开始)必须大于或等于零且小于参数列表的大小。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!