首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#中如何清空一个数组

2012-05-15 
c#中怎么清空一个数组using Systemusing System.Collections.Genericusing System.ComponentModelusing

c#中怎么清空一个数组
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{

  public partial class Form1 : Form
  {
   
  String[] stuname = new string[4];
  int [,] score=new int [4,6];
  int stuNum = 0;

  public Form1()
  {
  InitializeComponent();
  label0.Text = "输入第" + 1 + "个学生:";

  }

  private void button1_Click(object sender, EventArgs e)
  {
  stuname[stuNum]=textBox0.Text;
  score[stuNum, 0] = int.Parse(textBox1.Text);
  score[stuNum, 1] = int.Parse(textBox2.Text);
  score[stuNum, 2] = int.Parse(textBox3.Text);
  score[stuNum, 3] = int.Parse(textBox4.Text);
  score[stuNum, 4] = int.Parse(textBox5.Text);
  int total = 0;
  for (int i = 0; i < score.GetLength(1) - 1; i++)
  {
  total += score[stuNum , i];
   
  total /= score.GetLength(1) - 1;
  score[stuNum, 5] = total;
  stuNum++;
  label0.Text = "输入第" + (stuNum+1) + "个学生:";
  string str="";
  //点击输入显示之后 聚焦到textBox0 开始下一位学生成绩的输入
  textBox0.Text = " ";
  textBox1.Text = " ";
  textBox2.Text = " ";
  textBox3.Text = " ";
  textBox4.Text = " ";
  textBox5.Text = " ";
  textBox0.Focus();
  for (i = 0; i < score.GetLength(0); i++)
  {
  str += stuname[i] + "\t ";
  for (int j = 0; j < score.GetLength(1); j++)
  {
  str += score[i, j] + "\t ";

  }
  str += "\r\n";
  }
  textBox6.Text = str;
  if (stuNum >= stuname.GetLength(0))
  {
  textBox0.Enabled = false;
  textBox1.Enabled = false;
  textBox2.Enabled = false;
  textBox3.Enabled = false;
  textBox4.Enabled = false;
  textBox5.Enabled = false;
  button1.Enabled = false;
  }
  }
  }
  private void button2_Click(object sender, EventArgs e)
  {
   
  textBox0.Enabled = true;
  textBox1.Enabled = true;
  textBox2.Enabled = true;
  textBox3.Enabled = true;
  textBox4.Enabled = true;
  textBox5.Enabled = true;
  button1.Enabled = true;
  textBox6.Clear();
  String[] stuname = new string[4];
  int[,] score = new int[4, 6];
  //清空各个文本框
  textBox0.Text = " ";
  textBox1.Text = " ";
  textBox2.Text = " ";
  textBox3.Text = " ";
  textBox4.Text = " ";
  textBox5.Text = " ";


  textBox0.Focus();

  stuNum = 0;
  label0.Text = "输入第 1 个学生:";
   
  }

  private void button3_Click(object sender, EventArgs e)
  {
  // 判断是否退出
  DialogResult choice;
  choice = MessageBox.Show(" 你确定要退出吗?"," ", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
  if (choice == DialogResult.OK)
  {
  this.Close();
  Application.Exit();
  }


  }

   
  }
}


以上是源码 我的目的是在清空文本框之后 数组也被清空 现在的状态是 第二次输入的时候 只能覆盖掉第一次输入的

值 我想在第二次输入的时候 数组的值为0 和第一次输入的时候一样

最最重要的 刚开始学 最好不要太难懂的 谢谢

[解决办法]

探讨
数组.Initialize();

[解决办法]
上边是我的数组 下面是用Array.Clear得 按你上边的说法 第一个string数组应该是null值 但是运行不了 无法转换 我用了0 可以清空 不知是否我理解错误你的意思了

Clear的第二个参数是所要清除数组的起始索引,第二个是要清楚的元素数.

你上面的写法是对的,不会报错.

热点排行