郁闷了一上午了,帮忙看一下吧!!!!

phon 2004-11-21 01:51:20
string Item=",";
ArrayList myList=new ArrayList();
myList.Add("a");
myList.Add("b");
myList.Add("c");
myList.Add("c");
for(int i=0;i<myList.Count;i++)
{
if(myList[i].ToString()=="c")
Item+=myList[i].ToString();
}
string[] Items=Item.Split(new char[]{','});
for(int i=0;i<Items.Length;i++)
{
myList.Remove(Items[i].ToString());
Response.Write(Items[i].ToString());
}
Response.Write(myList.Count.ToString());

我的意思就是想把myList中的"c"去掉,可是为什么这样不能实现呢。有没有别的方法?
...全文
235 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuhaifeng558 2004-11-22
  • 打赏
  • 举报
回复
是呀
lengfeng8866 2004-11-22
  • 打赏
  • 举报
回复
奇怪啊,结贴了???我来晚了。。。
Hystericalboy 2004-11-21
  • 打赏
  • 举报
回复
出错的过程是这样的:
1.Item+=myList[i].ToString(); //得到的字符串是",cc"
2.string[] Items=Item.Split(new char[]{','}); //得到字符组""和"cc"
3.myList.Remove(Items[i].ToString()); //Items[0/1]里面值为""和"cc",制定删除元素名称错了,就导致myList其实并没有被改动
xaoxong 2004-11-21
  • 打赏
  • 举报
回复
其实你也可另重新定义一个ArrayList把!="c"的Add到新的ArraryList
Hystericalboy 2004-11-21
  • 打赏
  • 举报
回复
恩,对了,就在这里,我刚刚看错了不好意思
myList.Remove(Items[i].ToString());
这里Items里面只有两个:
Items[0]="";
Items[1]="cc";
随便你用myList.Remove(Items[0/1])肯定都不会把"c"除去,
phon 2004-11-21
  • 打赏
  • 举报
回复
To: Hystericalboy()

myList.Remove(Items[i].ToString()); //这里除去的不是"a"和"b"
Response.Write(Items[i].ToString()); //这里和ArraryList没有关系我知道,我Write出来就是想看看里面有没有我想去除掉的“C"...
Response.Write(myList.Count.ToString()); //这里返回是4,就证明没有把"C"去掉。
zpisgod 2004-11-21
  • 打赏
  • 举报
回复
很简单:

for(int i=0;i<myList.Count;i++)
{
if(myList[i].ToString()=="c")
{
myList.RemoveAt(i--); //注意这里就行了,删除一个myList.Count就减少了,所以让i-1;
}
}
Hystericalboy 2004-11-21
  • 打赏
  • 举报
回复
你的代码事实上的结果是:
... //第二个for语句里面的:
myList.Remove(Items[i].ToString()); //除去了ArrayList里的"a"和"b"
Response.Write(Items[i].ToString()); //返回了串string[]的""和"cc"(其实这里Response的内容和ArrayList内容已经没关系了)
...
Response.Write(myList.Count.ToString()); //返回了项数4
Hystericalboy 2004-11-21
  • 打赏
  • 举报
回复
你意思要去掉全部的c?
listhome 2004-11-21
  • 打赏
  • 举报
回复
你到底要实现什么呀!

都没有说明白,

删除就是Remove


或使用
ArrayList.RemoveRange
listhome 2004-11-21
  • 打赏
  • 举报
回复
帮你查了一下MSDN
MS帮你写好了关于查找的方法IndexOf

下面是例子!

using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList myAL = new ArrayList();
myAL.Add( "the" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumps" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
myAL.Add( "in" );
myAL.Add( "the" );
myAL.Add( "barn" );

// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList contains the following values:" );
PrintIndexAndValues( myAL );

// Search for the first occurrence of the duplicated value.
String myString = "the";
int myIndex = myAL.IndexOf( myString );
Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Search for the first occurrence of the duplicated value in the last section of the ArrayList.
myIndex = myAL.IndexOf( myString, 4 );
Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );

// Search for the first occurrence of the duplicated value in a section of the ArrayList.
myIndex = myAL.IndexOf( myString, 6, 6 );
Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 is at index {1}.", myString, myIndex );
}

public static void PrintIndexAndValues( IEnumerable myList ) {
int i = 0;
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
Console.WriteLine();
}
}
phon 2004-11-21
  • 打赏
  • 举报
回复
什么错误也不提示就是去除不到C
listhome 2004-11-21
  • 打赏
  • 举报
回复
Item是关键字,

而且是Obj型的吧!

他提示什么错误?
listhome 2004-11-21
  • 打赏
  • 举报
回复
Item+=myList[i].ToString();

这句不对吧!

phon 2004-11-21
  • 打赏
  • 举报
回复
UP!!!!!
xiaodao2008 2004-11-21
  • 打赏
  • 举报
回复
帮你顶一下吧~~
关注中.....
phon 2004-11-21
  • 打赏
  • 举报
回复
不用去判断先。上面的这个他明明是在里面的,可是我一个也没有Remove掉是为什么?
Response看都有c在里面。
Hystericalboy 2004-11-21
  • 打赏
  • 举报
回复
test这里是上面的myList
Hystericalboy 2004-11-21
  • 打赏
  • 举报
回复
加个方法判断是否还存在"c"在集合myList里面;
public bool check(string checkstring)
{
bool t=true;
foreach (string i in test)
{
if (i==checkstring)
t=false;
}
return t;
}

myList.Remove("c");一次只删除一个
phon 2004-11-21
  • 打赏
  • 举报
回复
myList.Remove("c")可以。
为什么我那样的却不行呢?
加载更多回复(1)

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧