|zyciis| 问个简单的数组问题,因为没有用过
数组我一直都没有用过一般都是用ArrayList但今天我想数据比这个要快一些,所以简单的问一下下面的程序换成数据要怎么做
ArrayList agentUserTypeList = new ArrayList();
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = (CheckBox)item.FindControl("chkAgentUserType");
agentUserTypeList.Add(chkAgentUserType.Text);
}
Response.Write(agentUserTypeList.Count.ToString());
谢谢 请转成String[] 写法 --------------------编程问答-------------------- 好像差不多吧?
--------------------编程问答-------------------- 范型的效率快~ 等下给你写例子 --------------------编程问答-------------------- string[] Zid = guidRE.Split(',');
for (int z = 0; z < Zid.Length; z++)
{
string CRR_Ex = "select * from CRR_Level where guid = '"+ Zid[z]+"'";
dt = DBOperate.GetDataTable(CRR_Ex);
if (dt.Rows.Count == 0)//沒有
{
string DelEx = "delete from CRR_EX where guid = '" + Zid[z] + "'";
objDBOperate.executeNonQuery (Comm,DelEx);
}
} --------------------编程问答--------------------
--------------------编程问答--------------------
string tempStr = string.Empty;
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = (CheckBox)item.FindControl("chkAgentUserType");
tempStr += chkAgentUserType.Text + ",.";
}
Response.Write(tempStr.Split(",.").Length.ToString());
List<string> agentUserTypeList = new List<string>();--------------------编程问答--------------------
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = (CheckBox)item.FindControl("chkAgentUserType");
agentUserTypeList.Add(chkAgentUserType.Text);
}
Response.Write(agentUserTypeList.Count.ToString());
--------------------编程问答--------------------
List<string> list=new List<string>();
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = item.FindControl("chkAgentUserType") as CheckBox;
if(chkAgentUserType!=null)
list.Add(chkAgentUserType.Text);
}
Response.Write(list.Count.ToString());
--------------------编程问答--------------------
using System.Collections.Generic;
--------------------编程问答-------------------- 是啊 List<string> items = new List<string> 很好用啊 --------------------编程问答--------------------
// 建议按我前面说的,用List<string>。如果非要用string[],则如下:
string[] agentUserTypeList = new string[dlAgentUserType.Items.Count];
for (int i = 0; i < dlAgentUserType.Items.Count; i++)
{
CheckBox chkAgentUserType = (CheckBox)dlAgentUserType.Items[i].FindControl("chkAgentUserType");
agentUserTypeList[i] = chkAgentUserType.Text;
}
Response.Write(agentUserTypeList.Length.ToString());
string[] s= new string[3];--------------------编程问答-------------------- @wuyi8808
s[0]="a";
s[1]="b";
s[2]="c";
foreach (string x in s)
{
Response.Write(x + "<br/>");
}
8楼的代码是偷懒的办法,会浪费资源.
LZ ,不好意思,我在4楼的代码是错误的,下面是正确的
--------------------编程问答-------------------- @Sandy945
string tempStr = string.Empty;
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = item.FindControl("chkAgentUserType") as CheckBox;
if(chkAgentUserType!=null)
tempStr += chkAgentUserType.Text + ",.";
}
Response.Write(tempStr.Split(",.".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length.ToString());
8楼的代码比11楼的代码好,不浪费资源。
11楼的代码使用了 tempStr += ...,严重浪费资源。如果需要连接字符串,应该使用 StringBuilder。
不过我建议使用5楼的代码。 --------------------编程问答-------------------- 學習。。。 --------------------编程问答-------------------- RE:
string[] agentUserTypeList = new string[dlAgentUserType.Items.Count];
for (int i = 0; i < dlAgentUserType.Items.Count; i++)
{
CheckBox chkAgentUserType = (CheckBox)dlAgentUserType.Items[i].FindControl("chkAgentUserType");
agentUserTypeList[i] = chkAgentUserType.Text;
}
Response.Write(agentUserTypeList.Length.ToString());
如果我是未知他有多少个的话要怎么办如
ArrayList agentUserTypeList = new ArrayList();
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = (CheckBox)item.FindControl("chkAgentUserType");
if(chkAgentUserType.Checked)
{
agentUserTypeList.Add(chkAgentUserType.Text);
}
}
Response.Write(agentUserTypeList.Count.ToString());
这样的话要怎么办 --------------------编程问答--------------------
@wuyi8808
正如你言,字符串拼接应该用StringBuilder ,
不过不是所有字符串拼接 中string+=的效率都比StringBuilder 低的
用+= 和Split 只是不想 多申请内存空间 --------------------编程问答--------------------
--------------------编程问答-------------------- one 和 two 的效率 取决于 CheckBox 的个数
//one
string tempStr = string.Empty;
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = item.FindControl("chkAgentUserType") as CheckBox;
if (chkAgentUserType != null)
tempStr += chkAgentUserType.Text + ",";
}
Response.Write(tempStr.Split(",").Length.ToString());
//two
System.Text.StringBuilder tempSb = new System.Text.StringBuilder();
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = item.FindControl("chkAgentUserType") as CheckBox;
if (chkAgentUserType != null)
tempSb.Append(chkAgentUserType.Text + ",");
}
Response.Write(tempSb.ToString().Split(",").Length.ToString());
少的时候 one 效率高
多的时候 two 效率高
推荐two
不过split(",")在这里有一个硬伤,就是 CheckBox 的文本不能包含 分割符,否则会和预想偏差 --------------------编程问答-------------------- 也就是说没有却定有多少个对像的时候就不可以用
String[] arr = new String[]
的这种方法了
是吗?
--------------------编程问答-------------------- 今天怎么这么迷糊呢~ 16楼的代码改为如下
//one
string tempStr = string.Empty;
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = item.FindControl("chkAgentUserType") as CheckBox;
if (chkAgentUserType != null)
tempStr += chkAgentUserType.Text + ",";
}
Response.Write(tempStr.TrimEnd(',').Split(",").Length.ToString());
//two
System.Text.StringBuilder tempSb = new System.Text.StringBuilder();
foreach (DataListItem item in dlAgentUserType.Items)
{
CheckBox chkAgentUserType = item.FindControl("chkAgentUserType") as CheckBox;
if (chkAgentUserType != null)
tempSb.Append(chkAgentUserType.Text + ",");
}
Response.Write(tempSb.ToString().TrimEnd(',').Split(",").Length.ToString());
需要去尾分割符 --------------------编程问答-------------------- 也就是说没有却定有多少个对像的时候就不可以用
String[] arr = new String[]
的这种方法了
是吗?
---------------
可以利用字符串的Split()函数 来创建数组 --------------------编程问答-------------------- 值类型的元素用泛型效率高很多,引用型的区别不是很大 --------------------编程问答-------------------- 没有确定有多少个对像的时候,建议使用 List<string>。其实既使在确定有多少个对像的时候,也建议使用 List<string>。
补充:.NET技术 , ASP.NET