获取列表项到数组 [英] Getting listbox items to array

查看:109
本文介绍了获取列表项到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public string[] getStopWords()
   {
      string[] stopWordArray = null;
      int itemCount = lbStopWords.Items.Count;

          for (int i = 0; i < itemCount; i++)
          {
              stopWordArray[i] = (string)lbStopWords.Items[i];
          }


       return stopWordArray;
   }




我写了上面的方法来将listBox项获取数组并返回数组,但是当使用该方法时,我得到了异常NullreferenceExeception被用户代码未处理

我找不到办法扔它.

还有其他方法可以做这种方法吗

请帮助




I wrote above method to get listBox items to array and return array but when using that method I am getting exception NullreferenceExeception was unhandle by user code

I couldn''t find a way to go throw it.

Is there any other way to do that kind of method

pls help

推荐答案

公共字符串[] get()
{
string [] arr =新字符串[listBox1.Items.Count];
for(int i = 0; i< listBox1.Items.Count; i ++)
{
arr [i] = listBox1.Items [i] .ToString();
}
return arr;
}


如果它解决了您的目的,请尝试使用它!!!
public string[] get()
{
string[] arr = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
arr[i] = listBox1.Items[i].ToString();
}
return arr;
}


try dis if it solve ur purpose!!


问题的根本原因是您使用的数组尚未确定尺寸.试试这个:

The root cause of your problem is that you are using an array that has not been dimensioned. Try this:

public string[] getStopWords()
{
    string[] stopWordArray = new string[lbStopWords.Items.Count];
    int itemCount = lbStopWords.Items.Count;
    for (int i = 0; i < itemCount; i++)
    {
        stopWordArray[i] = lbStopWords.Items[i].ToString();
    }
    return stopWordArray;
}


我知道您已经有了答案,但是您也可以使用:
I know you already have your answer, but you could also have used:
public string[] getStopWords()
       {
           string[] stopWordArray = new string[listBox1.Items.Count];
           listBox1.Items.CopyTo(stopWordArray,0);

           return stopWordArray;
       }


这篇关于获取列表项到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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