I would like to create an object array in C# of undefined length and then populate the array in a loop like so...
我想在C#中创建一个未定义长度的对象数组,然后像这样在循环中填充数组...
string[] splitWords = message.Split(new Char[] { ' ' });
Word[] words = new Word[];
int wordcount = 0;
foreach (string word in splitWords)
{
if (word == "") continue;
words[wordcount] = new Word(word);
wordcount++;
}
However, I get the error... "Array creation must have array size or array initializer"
但是,我得到错误......“数组创建必须有数组大小或数组初始化程序”
I'm doing a lot more logic in the foreach loop that I've left out for brevity.
我在foreach循环中做了很多逻辑,我为了简洁而遗漏了。
7 个解决方案
#1
40
What you want to do is create:
你想要做的是创造:
List<Word> words = new List<Word>();
and then:
接着:
words.Add(new Word(word));
And finally when the loop is done if you need an array:
最后,当你需要一个数组时完成循环:
words.ToArray();
#2
9
If you're using C# 3.5, you can just do the following.
如果您使用的是C#3.5,则可以执行以下操作。
var words = message
.Split(new char[]{' '})
.Where(x => x != "")
.Select(x => new Word(x))
.ToArray();
#3
6
You can't create an array of undefined length. This is where you'd use a generic List.
您无法创建未定义长度的数组。这是您使用通用列表的地方。
List<Word> words = new List<Word>();
#4
1
A friendly note, you can pass option to split to ignore empty entries. Assuming no other logic to prune out entries you can preinitialize your array like so:
友情提示,您可以通过选项拆分以忽略空条目。假设没有其他逻辑来删除条目,您可以预先初始化您的数组,如下所示:
string[] splitWords = message.Split(new Char[] {' '},
StringSplitOptions.RemoveEmptyEntries);
Word[] words = new Word[splitWords.Length];
#5
0
Actually you may use list to populate your words first and then convert it easily to array like this:
实际上你可以使用list首先填充你的单词,然后将它轻松转换为数组,如下所示:
string[] splitWords = message.Split(new Char[] { ' ' });
List<Word> words = new List<Word>();
int wordcount = 0;
foreach (string word in splitWords)
{
if (word == "") continue;
words.add(new Word(word));
//wordcount++;
}
wordcount = words.count;
return words.ToArray();
#6
0
I'm wondering why can't we just use a string variable (say x
), initialize it and retrieve comma separated data in it and later use string[]
variable (say y[]
) and initialize it equal to x.Split(',')
without having to initialize a blank array like follows:
我想知道为什么我们不能只使用字符串变量(比如x),初始化它并在其中检索逗号分隔数据,然后使用string []变量(比如y [])并初始化它等于x.Split( ',')无需初始化空白数组,如下所示:
string x = string.Empty;
string msg = "hi,how,r,u,xyz";
void Page_Load(object sender, EventArgs e)
x = msg;
string[] y = msg.Split(',');
I think this should work as needed but I didn't try running this so I am not sure. If someone thinks that my solution is wrong, please correct me.
我认为这应该按照需要工作,但我没有尝试运行这个,所以我不确定。如果有人认为我的解决方案有误,请纠正我。
#7
-2
I solved it by using an ArrayList and then casting it to the object array after iterating...
我通过使用ArrayList解决了它,然后在迭代后将其转换为对象数组...
string[] splitWords = message.Split(new Char[] {' '});
ArrayList wordList = new ArrayList();
int wordcount = 0;
foreach (string word in splitWords)
{
if (word == "") continue;
Word newWord = new Word(word);
wordList.Add(newWord);
wordcount++;
}
Word[] words = (Word[])wordList.ToArray(typeof(Word));
I've heard the whole "create question/answer just to document it for others" is acceptable. Plus I'd like to hear if there are better suggestions. Thanks.
我听说整个“创建问题/答案只是为其他人记录”是可以接受的。另外,我想听听是否有更好的建议。谢谢。
#1
40
What you want to do is create:
你想要做的是创造:
List<Word> words = new List<Word>();
and then:
接着:
words.Add(new Word(word));
And finally when the loop is done if you need an array:
最后,当你需要一个数组时完成循环:
words.ToArray();
#2
9
If you're using C# 3.5, you can just do the following.
如果您使用的是C#3.5,则可以执行以下操作。
var words = message
.Split(new char[]{' '})
.Where(x => x != "")
.Select(x => new Word(x))
.ToArray();
#3
6
You can't create an array of undefined length. This is where you'd use a generic List.
您无法创建未定义长度的数组。这是您使用通用列表的地方。
List<Word> words = new List<Word>();
#4
1
A friendly note, you can pass option to split to ignore empty entries. Assuming no other logic to prune out entries you can preinitialize your array like so:
友情提示,您可以通过选项拆分以忽略空条目。假设没有其他逻辑来删除条目,您可以预先初始化您的数组,如下所示:
string[] splitWords = message.Split(new Char[] {' '},
StringSplitOptions.RemoveEmptyEntries);
Word[] words = new Word[splitWords.Length];
#5
0
Actually you may use list to populate your words first and then convert it easily to array like this:
实际上你可以使用list首先填充你的单词,然后将它轻松转换为数组,如下所示:
string[] splitWords = message.Split(new Char[] { ' ' });
List<Word> words = new List<Word>();
int wordcount = 0;
foreach (string word in splitWords)
{
if (word == "") continue;
words.add(new Word(word));
//wordcount++;
}
wordcount = words.count;
return words.ToArray();
#6
0
I'm wondering why can't we just use a string variable (say x
), initialize it and retrieve comma separated data in it and later use string[]
variable (say y[]
) and initialize it equal to x.Split(',')
without having to initialize a blank array like follows:
我想知道为什么我们不能只使用字符串变量(比如x),初始化它并在其中检索逗号分隔数据,然后使用string []变量(比如y [])并初始化它等于x.Split( ',')无需初始化空白数组,如下所示:
string x = string.Empty;
string msg = "hi,how,r,u,xyz";
void Page_Load(object sender, EventArgs e)
x = msg;
string[] y = msg.Split(',');
I think this should work as needed but I didn't try running this so I am not sure. If someone thinks that my solution is wrong, please correct me.
我认为这应该按照需要工作,但我没有尝试运行这个,所以我不确定。如果有人认为我的解决方案有误,请纠正我。
#7
-2
I solved it by using an ArrayList and then casting it to the object array after iterating...
我通过使用ArrayList解决了它,然后在迭代后将其转换为对象数组...
string[] splitWords = message.Split(new Char[] {' '});
ArrayList wordList = new ArrayList();
int wordcount = 0;
foreach (string word in splitWords)
{
if (word == "") continue;
Word newWord = new Word(word);
wordList.Add(newWord);
wordcount++;
}
Word[] words = (Word[])wordList.ToArray(typeof(Word));
I've heard the whole "create question/answer just to document it for others" is acceptable. Plus I'd like to hear if there are better suggestions. Thanks.
我听说整个“创建问题/答案只是为其他人记录”是可以接受的。另外,我想听听是否有更好的建议。谢谢。