比如声明了个全局变量数组,int [] y= new int [10];
到了后面发现需要增加,int[] y=new ing[20];该如何写啊?

解决方案 »

  1.   

    private void AddNewsInfo()
    {

    ArrayList arr;
    try
    {
    arr=new ArrayList();
    if(this.DropDownListClass.Items.Count==0)
    {arr.Add(0);}
    else
    {arr.Add(this.DropDownListClass.SelectedItem.Value.ToString());}
    if(this.DropDownListNclass.Items.Count==0)
    {arr.Add(0);}
    else
    {arr.Add(this.DropDownListNclass.SelectedItem.Value.ToString());}
    if(this.DropDownListmclass.Items.Count==0)
    {arr.Add(0);}
    else
    {arr.Add(this.DropDownListmclass.SelectedItem.Value.ToString());}
    StringWriter writer1 = new StringWriter();
    Server.HtmlDecode(this.title.Text,writer1);
    String NewsTitle = writer1.ToString();
    arr.Add(NewsTitle);
    StringWriter writer2 = new StringWriter();
    Server.HtmlDecode(this.author.Text,writer2);
    String Author = writer2.ToString();
    arr.Add(Author);
    arr.Add(this.edituser.Text.Trim());
    StringWriter writer3 = new StringWriter();
    Server.HtmlEncode(this.content1.Value.Trim(),writer3);
    String Content = writer3.ToString();
    arr.Add(Content);
    arr.Add("url");
    arr.Add(System.DateTime.Now);
    arr.Add("no");
    arr.Add("");
    arr.Add(Session["liushuihao"].ToString());
    if (this.CheckBox1.Checked==true)
    {
    arr.Add(System.DateTime.Now.AddYears(10).ToShortDateString());
    }
    else
    {
    arr.Add(this.trem.Text);
    }
    arr.Add(this.deptname.Text);
    StringWriter writer4 = new StringWriter();
        Server.HtmlDecode(this.correlation.Text,writer4);
    String Correlation = writer4.ToString();
    arr.Add(Correlation);
    myNewsClass.AddNews(arr);
    }
    catch(Exception er)
    {
    throw new Exception(er.Message);
    }

    }
      

  2.   

    C#没有可变长数组,需要就用队列ArrayList,具体例子去MSDN里面索引-〉ArrayList
      

  3.   

    量大没有关系的。不过如果你能预知大概数量的话,在New的时候添加上数量,因为重新划分空间可能会对性能有一定影响,不过也不大。
      

  4.   

    ArrayList
    里面能不能存放数据流啊?
      

  5.   

    非洲你到底要干嘛?要让数组有动态的长度就用ArrayList,要使用流就用流,流的buffer长度也是可以变化的。
      

  6.   

    我想在程序一开始时就定义个数组,也就是全局数组,但是一开始不知道数组应该设多少空间,如果后面不够,想重新定义数组,增加数组的大小,象VB里面就有个Redim,是用来在过程级用于重新分配数组变量的存储空间,请问C#里面有没对应的函数?谢谢!
    我看过ArrayList,发现只能增加字符串啊?好象没有增加数组的例子啊!
    各位高手能举个数组例子吗?谢谢了!
      

  7.   

    ArrayList 能存object ,那它就可以存整个宇宙!!!
      

  8.   

    我查了MSDN,没有REMALLOC啊??
      

  9.   

    不知道你看了《C#入门经典》没有,里面有个例子就是一种简单的动态数组,但是全局数组是不能一开始就初始化的!说简单点就是再建2个局部数组,元素个数分别用A,B确定,再用个变量C来做中间量。剩下的就是A=B,B=C,C=A之类的转换,在把C传回给全局数组。
    不知道这种方法好不好,不过你可以试下!
      

  10.   

    ArrayList,而且还可以存放Object。
      

  11.   

    不用数组,用集合,ArrayList或者Hashtable都可以。集合不用规定长度的。比如
    Hashtable table = new Hashtable();
    table("1").value = "first";
    ……
      

  12.   

    [C#] 
    using System;
    using System.Collections;
    public class SamplesHashtable  {   public static void Main()  {      // Creates and initializes a new Hashtable.
          Hashtable myHT = new Hashtable();
          myHT.Add( "one", "The" );
          myHT.Add( "two", "quick" );
          myHT.Add( "three", "brown" );
          myHT.Add( "four", "fox" );      // Displays the Hashtable.
          Console.WriteLine( "The Hashtable contains the following:" );
          PrintKeysAndValues( myHT );
       }
       public static void PrintKeysAndValues( Hashtable myList )  {
          IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
          Console.WriteLine( "\t-KEY-\t-VALUE-" );
          while ( myEnumerator.MoveNext() )
             Console.WriteLine( "\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value );
          Console.WriteLine();
       }
    }
    /* 
    This code produces the following output.The Hashtable contains the following:
        -KEY-    -VALUE-
        three:    brown
        four:    fox
        two:    quick
        one:    The
    */ 
      

  13.   

    Vb.Net 有REDim.C#就不太知道了。
      

  14.   

    using System;namespace Cybenaute 
    {
    class Test
    {
    public void MyArray(int size)
    {
    myArray = new int[size];
    for(int i=0; i<myArray.Length; i++)
    {
    myArray[i] = i;
    }
    for(int j=0; j<myArray.Length; j++)
    {
    Console.WriteLine("myArray[{0}]={1}", j, myArray[j]);
    }
    }
    private int[] myArray;
    }
    class ArrTest
    {
    public static void Main(string[] Args)
    {
    Test test = new Test();
    test.MyArray(10);
    test.MyArray(2000);
    }
    }
    }
      

  15.   

    ArrayList里存放的是对象的引用,什么都可以放的流当然可以,而且可以放各种不同的东西
    比如
    objArr.Add("hello");
    objArr.Add(objDataSet);
    objArr.Add(new Stream(...));
      

  16.   

    问题解决了,先申明一个数组,
    int []a;
    然后在需要时在重新附值,不过也很谢谢大家!结贴!