怎样重定义数组?在线等,急急急!!! 比如声明了个全局变量数组,int [] y= new int [10];到了后面发现需要增加,int[] y=new ing[20];该如何写啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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); } } C#没有可变长数组,需要就用队列ArrayList,具体例子去MSDN里面索引-〉ArrayList 量大没有关系的。不过如果你能预知大概数量的话,在New的时候添加上数量,因为重新划分空间可能会对性能有一定影响,不过也不大。 ArrayList里面能不能存放数据流啊? 非洲你到底要干嘛?要让数组有动态的长度就用ArrayList,要使用流就用流,流的buffer长度也是可以变化的。 我想在程序一开始时就定义个数组,也就是全局数组,但是一开始不知道数组应该设多少空间,如果后面不够,想重新定义数组,增加数组的大小,象VB里面就有个Redim,是用来在过程级用于重新分配数组变量的存储空间,请问C#里面有没对应的函数?谢谢!我看过ArrayList,发现只能增加字符串啊?好象没有增加数组的例子啊!各位高手能举个数组例子吗?谢谢了! ArrayList 能存object ,那它就可以存整个宇宙!!! 我查了MSDN,没有REMALLOC啊?? 不知道你看了《C#入门经典》没有,里面有个例子就是一种简单的动态数组,但是全局数组是不能一开始就初始化的!说简单点就是再建2个局部数组,元素个数分别用A,B确定,再用个变量C来做中间量。剩下的就是A=B,B=C,C=A之类的转换,在把C传回给全局数组。不知道这种方法好不好,不过你可以试下! ArrayList,而且还可以存放Object。 不用数组,用集合,ArrayList或者Hashtable都可以。集合不用规定长度的。比如Hashtable table = new Hashtable();table("1").value = "first";…… [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*/ Vb.Net 有REDim.C#就不太知道了。 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); } }} ArrayList里存放的是对象的引用,什么都可以放的流当然可以,而且可以放各种不同的东西比如objArr.Add("hello");objArr.Add(objDataSet);objArr.Add(new Stream(...)); 问题解决了,先申明一个数组,int []a;然后在需要时在重新附值,不过也很谢谢大家!结贴! 求一个比较好的datagridview vpn网络,用socket通信,连接不上 c#中,list,innerlist以及this,到底指什么? 大家来看一下,这是不是一个倒分帖 如何让一个窗口始终在另一个窗口上面? WPF中关于ListView的问题 .net 急!!同一解决方案内的多个项目之间如何引用? sql数据库恢复问题。急!各位高手帮忙 DataSet怎样导出Excel文件? 我想用C#或其他语言写一个UML里的画class diagram的工具,请各位提供资料或源码更好 C#中如何调用delphi写的DLL程序
{
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);
}
}
里面能不能存放数据流啊?
我看过ArrayList,发现只能增加字符串啊?好象没有增加数组的例子啊!
各位高手能举个数组例子吗?谢谢了!
不知道这种方法好不好,不过你可以试下!
Hashtable table = new Hashtable();
table("1").value = "first";
……
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
*/
{
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);
}
}
}
比如
objArr.Add("hello");
objArr.Add(objDataSet);
objArr.Add(new Stream(...));
int []a;
然后在需要时在重新附值,不过也很谢谢大家!结贴!