在数组中存储字符串时出错。适用于整数但不适用于字符串。有什么建议吗? [英] Error in storing strings in an array. Works for integer but not for string. Any suggestion?

查看:96
本文介绍了在数组中存储字符串时出错。适用于整数但不适用于字符串。有什么建议吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 WindowsFormsApplication1
{
public partial class Form1:Form
{

public Form1()
{
InitializeComponent();
}

private const int id = 10 ;

private string [] numArray = new string [id];
private int index = 0 ;



private void button1_Click( object sender,EventArgs e)
{
{
if this .index < 10
{
numArray [ this .index] = textBox1.Text;
this .index ++;
}
}
}

private void button2_Click( object sender,EventArgs e)
{
for int i = 0 ; i < 10 ; i ++)
{

listBox1.Items.Add(numArray [i]); // 在列表框中显示数组

}
}

private void button3_Click( object sender,EventArgs e)
{
textBox1.Clear();
}
}
}





从评论中移到此处:



嗯,实际上我只是看到它能正常工作,如果我输入10个字符串,但如果输入的话甚至可能还不到那么我希望它能工作..

这就是为什么我得到的错误,它包含一个空值

解决方案

问题是它确实有效:它正在努力做到你也说了什么!



但是......整数和字符串是不同的:整数是值类型,字符串是引用类型。



所以当你创建一个整数数组时:

  int  [ ] myInts =  new   int  [ 10 ] ; 

你得到一个十个整数的数组,用默认值填充,这是零;

但字符串的代码相同:

< pre lang =c#> string [] myStrings = new string [ 10 ];



创建一个包含10个字符串引用的数组,用Referance类型填充teh defautl值,是 null



请改为尝试:

  for  int  i =  0 ; i <  index; i ++)
{
listBox1.Items.Add(numArray [i]); // 在列表框中显示数组
}


要避免空引用异常,请使用空字符串初始化表单构造函数中的数组索引(在InitializeComponent()之后)。



如果您使用List< string>而不是数组,你可以完全绕过这个问题,因为你不必预先指定List的大小,它将具有你添加到它的字符串的大小。



请参阅:

https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx [ ^ ]

http://www.dotnetperls.com/list [ ^ ]


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private const int id = 10;

        private string[] numArray = new string[id];
        private int index = 0;



          private void button1_Click(object sender, EventArgs e)
          {
              {
                  if (this.index < 10)
                  {
                      numArray[this.index] = textBox1.Text;
                      this.index++;
                  }
              }
          }

          private void button2_Click(object sender, EventArgs e)
          {
              for (int i = 0; i < 10; i++)
              {

                  listBox1.Items.Add(numArray[i]); //show array in a listbox

              }
          }

          private void button3_Click(object sender, EventArgs e)
          {
              textBox1.Clear();
          }
    }
}



Moved here from comment:

Um yeah actually I just saw that it works if I input exactly 10 strings but I want it to work if I input maybe even less than that..
That was why I was getting the error that it contains a null value

解决方案

The problem is that it does work: it is trying to do exactly what you told it too!

But...integers and strings are different: integers are Value types, and strings are Reference types.

So when you create an array of integers:

int[] myInts = new int[10];

You get an array of ten integers, filled with the default value, which is zero;
But the same code for strings:

string[] myStrings = new string[10];


Creates an array of ten references to strings, filled with teh defautl value for a Referance type, which is null

Try this instead:

for (int i = 0; i < index; i++)
    {
    listBox1.Items.Add(numArray[i]); //show array in a listbox
    }


To avoid the null-reference exception, initialize the array-indices in the constructor of your form with empty strings (after InitializeComponent()).

If you would use a List<string> instead of the array, you could circumvent that problem altogether, because you don't have to specify the size of the List beforehand and it will have exactly the size of how many strings you've added to it.

See:
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^]
http://www.dotnetperls.com/list[^]


这篇关于在数组中存储字符串时出错。适用于整数但不适用于字符串。有什么建议吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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