定义一个动态数组:List<float> a1 = new List<float>(10);
赋值a1.add(0);
这样我认为a1的元素应为10个,5、0.0、0.0、0.0、0.0、0.0、.......
但使用a1[0]不超界,a1[1]就超界了,应该是a1中只有5,
如果按程序要求必须判断动态数组a1[1]是否为被填入数据该怎么判断呢?
可以用别的动态数组,但必须是动态的,因为事先不知道该盛多少数据,有什么好办法呢,求求各位了

解决方案 »

  1.   

    List <float> a1 = new List <float>();
      

  2.   

    通过个数来判断有多少无素被加入值了.arraylist.Count
      

  3.   

    private void button1_Click(object sender, EventArgs e)
    {
        List<float> a1 = new List<float>();
        a1.Add(5.0F);
        a1.Add(0);
        a1.Add(0);
        a1.Add(0);    for (int i = 0; i < a1.Count; i++)
        {
            MessageBox.Show(a1[i].ToString());
        }
    }
      

  4.   

    List<float> a1;
    int listCount = 0; //当前值
    private bool IsAdded()
    {
        if (a1.Count > listCount)
        {
            listCount = a1.Count;
            return true;
        }
        else return false;
    }
      

  5.   

    在3.0以上,可以给list<float>加个扩展方法来统计,当然,你也可以放外头统计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 ListExMethodNS
    {
        public static class ListExMethod
        {
            static int TotalCount = 0;        public static int AddEx(this List<double> L, double F)
            {
                L.Add(F);
                return ++TotalCount;
            }
        }
    }namespace WindowsFormsApplication24
    {
        using ListExMethodNS;    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                List<double> L = new List<double>(10);            MessageBox.Show(L.AddEx(5).ToString());
                MessageBox.Show(L.AddEx(8).ToString());
                MessageBox.Show(L.AddEx(10.5).ToString());
            }
        }
    }
      

  6.   

    凭我的水平我实在看不懂您究竟在问什么,郁闷!!赋值a1.add(0); 怎么第一个元素会变5??提醒你,必须要自己初始化!!
      

  7.   

    分配好空间不见得就直接可用了,就象
    int a,b;
    a=b;
    确保出错