Arraylist.remove问题?请帮忙。 [英] Arraylist.remove problem?? Please help.
问题描述
只要arraylist是>我就有一个设置为运行的循环。 0.
在这个循环开始的
我抓住第一个对象然后
删除它。然后我进入另一个循环,检查是否有更多的对象与我抓住的第一个对象匹配。如果
匹配,那么我把它们放在一个数组中。我想从arraylist中删除每个
的比赛,因为我发现它们可以加快速度,所以
他们不会再次检查。如果我试着这样做,那么似乎要把一切搞得一团糟。请有人帮助我。它们也不是我正在做的完全匹配...它们是对象,我做了一个正则表达式
我抓住的第一个对象然后检查每个对象看看它是否b
$
--------- -------------------------------------
发布于NewsLeecher v3。 0决赛
*二元Usenet轻松取笑
* http://www.newsleecher.com/?usenet
---------------------- ------------------------
I have a loop that is set to run as long as the arraylist is > 0.
at the beginning of this loop I grab the first object and then
remove it. I then go into another loop that checks to see if there
are more objects that match the first object that i grabbed. If
they match then I put them in an array. I would like to remove each
match from the arraylist as I find them to speed things up and so
that they don''t get checked again. If I try to do that it seems to
mess everything up. Please can someone help me. Also they are not
exact matches that I am doing...they are objects and I do a regex on
the first object that i grab and then check each one to see if it
contains the new regexed value in a varaible of each object.
--
----------------------------------------------
Posted with NewsLeecher v3.0 Final
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------
推荐答案
Ex*******@extremest.com 写道:
只要arraylist是>我就有一个设置为运行的循环。在这个循环开始时我抓住了第一个对象,然后将其删除。然后我进入另一个循环,检查是否有更多的对象与我抓住的第一个对象相匹配。如果它们匹配,那么我把它们放在一个数组中。我想从arraylist中删除每个
匹配,因为我发现它们可以加快速度,所以
它们不会再被检查。如果我试着这样做,似乎把一切搞得一团糟。请有人帮助我。它们也不是我正在做的完全匹配......它们是对象,我做了一个正则表达式,我抓住的第一个对象,然后检查每个对象,看它是否包含
每个对象的变量中新的正则表达式值。
-
I have a loop that is set to run as long as the arraylist is > 0.
at the beginning of this loop I grab the first object and then
remove it. I then go into another loop that checks to see if there
are more objects that match the first object that i grabbed. If
they match then I put them in an array. I would like to remove each
match from the arraylist as I find them to speed things up and so
that they don''t get checked again. If I try to do that it seems to
mess everything up. Please can someone help me. Also they are not
exact matches that I am doing...they are objects and I do a regex on
the first object that i grab and then check each one to see if it
contains the new regexed value in a varaible of each object.
--
这听起来像是一个相当小的代码片段。
你能把它解压成一个小的,独立的程序吗?
演示问题,然后在这里发布整个东西,这样我们就可以粘贴
了它进入VS并为自己尝试了吗?
It sounds like a fairly small stretch of code that is the problem.
Could you extract it into a small, stand-alone program that
demonstrates the problem and then post the whole thing here, so that we
can paste it into VS and try it for ourselves?
确定我是c#的新手,所以请放轻松对待我....大声笑..
这里是整个前卫。它是一个控制台应用程序,所以不是很大...只有
今天写了它所以它仍然相当草率...如果有更好的
的做法其中任何内容请告诉我。
使用系统;
使用System.Collections;
使用System.Text;
使用MySql.Data;
使用System.Text.RegularExpressions;
命名空间createfiles
{
class program
{
static MySql.Data.MySqlClient.MySqlConnection conn
= new MySql.Data.MySqlClient.MySqlConnection( );
static MySql.Data.MySqlClient.MySqlCommand cmd =
new MySql.Data.MySqlClient.MySqlCommand();
静态字符串myConnectionString =" server =
127.0.0.1; uid = root; pwd = password; database = test;" ;;
static ArrayList master;
静态字符串组;
静态字符串表;
静态字符串[] groups = {
" alt.binaries.games.xbox" ;,alt.bi naries.games.xbox360",
" alt.binaries.vcd" };
static Regex reg = new Regex(" \\。");
static Regex seg = new Regex(" \\( [0-9] * / [0-9]。*
\\)",RegexOptions.IgnoreCase);
struct Header
{
公共字符串麻木;
公共字符串主题;
公共字符串日期;
公共字符串来自;
公共字符串msg_id;
公共字符串字节;
}
static void Main (string [] args)
{
for(int x = 2; x< groups.Length; x ++)
{
table = reg.Replace(groups [x],"");
group = groups [x];
getheaders();
Console.WriteLine(有这么多标题
{0},master.Count);
标题一=(标题) )master [0];
Console.WriteLine(" first one {0} {1}",
one.numb,one.subject);
find();
master.Clear();
}
}
static void getheaders()
{
conn.ConnectionString = myConnectionString;
conn.Open();
cmd.Connection = conn;
cmd.CommandText =" select * from" + table +"
其中主题如''%(%/%)%''按主题desc排序';
MySql.Data.MySqlClient.MySqlDataReader reader;
reader = cmd.ExecuteReader();
标题h =新标题();
master = new ArrayList();
while(reader.Read())
{
h.numb = reader.GetValue(0).ToString();
h.subject = reader.GetValue(1).ToString();
h.from = reader.GetValue(2).ToString();
h .date = reader.GetValue(3).ToString();
h.msg_id = reader.GetValue(4).ToString();
h.bytes = reader .GetValue(5).ToString();
master.Add(h);
}
reader.Close();
conn.Close();
}
static void find()
{
int foundm = 0;
while(master.Count> 0)
{
Header start =(Header) master [0];
master.RemoveAt(0);
匹配m = seg.Match(start.subject);
string segsplit = m.ToString();
segsplit = segsplit.Replace("(","");
segsplit = segsplit.Replace(" )","");
string [] segments = segsplit.Split(''/'');
int max = int.Parse(segments [1]);
max + = 1;
int counter = 1;
Header [] found = new Header [max];
int index = int.Parse(segments [0]);
int temp = master.Count;
if(index< max)
{
found [index] = start;
for(int x = 0; x< master.Count; x ++)
{
页眉测试=(页眉)大师[x];
字符串testsubject = seg.Replace
(start.subject,"");
if(test.subject.Contains
(testsubject))
{
//master.Remove(test);
匹配t = seg.Match
(test.subject);
string tsplit = t.ToString();
string tsegsplit =
tsplit.Replace("(","");
tsgsplit = tsegsplit.Replace
(")","");
string [] tsegments =
tsegsplit .Split(''/'');
index = int.Parse(tsegments
[0]);
//控制台。 WriteLine(柜台);
if(index< max)
{
found [index] = test;
counter ++;
}
}
}
//Console.WriteLine(" ; counter = {0}",
counter);
int testmax = max-1;
if(counter == testmax)
{
foundm ++;
for(int t = 1; t< found.Length;
t ++)
{
Console.WriteLine(" We have a
Match { 0}",找到[t] .subject);
}
}
}
} < br $>
}
}
}
-
- --------------------------------------------
发布于NewsLeecher v3.0 Final
* Binary Usenet Leching Made easy
* http://www.newsleecher.com/?usenet
--------------- -------------------------------
ok I am new to c# so take it easy on me....lol..
here is the whole prog. It is a console app so is not to big...only
wrote it today so it is still pretty sloppy...if there is a better
way of doing anything in it please let me know.
using System;
using System.Collections;
using System.Text;
using MySql.Data;
using System.Text.RegularExpressions;
namespace createfiles
{
class Program
{
static MySql.Data.MySqlClient.MySqlConnection conn
= new MySql.Data.MySqlClient.MySqlConnection();
static MySql.Data.MySqlClient.MySqlCommand cmd =
new MySql.Data.MySqlClient.MySqlCommand();
static string myConnectionString = "server=
127.0.0.1;uid=root;pwd=password;database=test;";
static ArrayList master;
static string group;
static string table;
static string[] groups = {
"alt.binaries.games.xbox", "alt.binaries.games.xbox360",
"alt.binaries.vcd" };
static Regex reg = new Regex("\\.");
static Regex seg = new Regex("\\([0-9]*/[0-9].*
\\)",RegexOptions.IgnoreCase);
struct Header
{
public string numb;
public string subject;
public string date;
public string from;
public string msg_id;
public string bytes;
}
static void Main(string[] args)
{
for (int x = 2; x < groups.Length; x++)
{
table = reg.Replace(groups[x], "");
group = groups[x];
getheaders();
Console.WriteLine("Have this many headers
{0}", master.Count);
Header one = (Header)master[0];
Console.WriteLine("first one {0} {1}",
one.numb, one.subject);
find();
master.Clear();
}
}
static void getheaders()
{
conn.ConnectionString = myConnectionString;
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "select * from " + table + "
where subject like ''%(%/%)%'' order by subject desc";
MySql.Data.MySqlClient.MySqlDataReader reader;
reader = cmd.ExecuteReader();
Header h = new Header();
master = new ArrayList();
while (reader.Read())
{
h.numb = reader.GetValue(0).ToString();
h.subject = reader.GetValue(1).ToString();
h.from = reader.GetValue(2).ToString();
h.date = reader.GetValue(3).ToString();
h.msg_id = reader.GetValue(4).ToString();
h.bytes = reader.GetValue(5).ToString();
master.Add(h);
}
reader.Close();
conn.Close();
}
static void find()
{
int foundm = 0;
while (master.Count > 0)
{
Header start = (Header)master[0];
master.RemoveAt(0);
Match m = seg.Match(start.subject);
string segsplit = m.ToString();
segsplit = segsplit.Replace("(", "");
segsplit = segsplit.Replace(")", "");
string[] segments = segsplit.Split(''/'');
int max = int.Parse(segments[1]);
max += 1;
int counter = 1;
Header[] found = new Header[max];
int index = int.Parse(segments[0]);
int temp = master.Count;
if (index < max)
{
found[index] = start;
for (int x = 0; x < master.Count; x++)
{
Header test = (Header)master[x];
string testsubject = seg.Replace
(start.subject, "");
if (test.subject.Contains
(testsubject))
{
//master.Remove(test);
Match t = seg.Match
(test.subject);
string tsplit = t.ToString();
string tsegsplit =
tsplit.Replace("(", "");
tsegsplit = tsegsplit.Replace
(")", "");
string[] tsegments =
tsegsplit.Split(''/'');
index = int.Parse(tsegments
[0]);
//Console.WriteLine(counter);
if (index < max)
{
found[index] = test;
counter++;
}
}
}
//Console.WriteLine("counter = {0}",
counter);
int testmax = max-1;
if (counter == testmax)
{
foundm++;
for (int t = 1; t < found.Length;
t++)
{
Console.WriteLine("We Have a
Match {0}", found[t].subject);
}
}
}
}
}
}
}
--
----------------------------------------------
Posted with NewsLeecher v3.0 Final
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------
我看不出你的代码有什么特别的错误。 RemoveAt(0)
应该每次在主循环周围移除一个项目,因此数组
最终应该变空。
我建议在调试器下运行你的程序并观察
它的功能。很可能你很快就会看到这个错误。
不幸的是我无法测试它,因为我不是我安装了MySql。
我做了一些重新安排,以便更容易阅读/调试。警告:我
无法编译此代码,因为我没有MySql,所以我没有
保证它将编译/运行。我还将你的Header
结构改为了一个类。我认为使用struct没有任何优势。对于Header。
以下是重新排列的代码。
使用System;
使用System.Collections;
使用System.Text;
使用MySql.Data;
使用System.Text.RegularExpressions;
命名空间createfiles
{
class program
{
static string [] groups = {
" alt .binaries.games.xbox"," alt.binaries.games.xbox360",
" alt.binaries.vcd" };
静态正则表达式reg =新正则表达式(" \\。");
静态正则表达式seg =新
正则表达式(\\([0-9] * / [0-9]。* \\)",RegexOptions.IgnoreCase);
类标题
{
公共字符串麻木;
公共字符串主题;
公共字符串日期;
公共字符串来自;
公共字符串msg_id;
公共字符串字节;
}
static void Main(string [] args)
{
for(int x = 2; x< groups.Length; x ++)
{
string table = reg.Replace(groups [x],"");
ArrayList master = getheaders(table);
Console.WriteLine("有这么多标题{0}",master.Count);
标题一=(标题)主[0];
Console.WriteLine("第一个) {0} {1}",one.numb,one.subject);
find(master);
}
}
静态ArrayList getheaders(字符串表)
{
MySql.Data.MySqlClient.MySqlConnection conn
= new MySql.Data.MySqlClient.MySqlConnection();
MySql .Data.MySqlClient.MySqlCommand cmd =
new MySql.Data.MySqlClient.MySqlCommand();
string myConnectionString =
" server = 127.0.0.1; uid = root; pwd = password; database = t est;" ;;
conn.ConnectionString = myConnectionString;
conn.Open();
cmd.Connection = conn;
cmd.CommandText =" select * from" + table +"主题如
''%(%/%)%''按主题desc排序" ;;
MySql.Data.MySqlClient.MySqlDataReader reader;
reader = cmd.ExecuteReader();
标题h =新标题();
ArrayList master = new ArrayList();
while(reader.Read())
{
h.numb = reader.GetValue(0).ToString();
h .subject = reader.GetValue(1).ToString();
h.from = reader.GetValue(2).ToString();
h.date = reader .GetValue(3).ToString();
h.msg_id = reader.GetValue(4).ToString();
h.bytes = reader.GetValue(5 ).ToString();
master.Add(h);
}
reader.Close();
conn.Close();
返回主服务器;
}
static void find(ArrayList master)
{
int foundm = 0;
while(master.Count> 0)
{
标题start =(Header)master [0];
master.RemoveAt(0);
int index;
int max;
ExtractMessageNumber(start.subject,out index,out max);
max + = 1;
int counter = 1;
Header [] found = new Header [max];
int temp = master.Count;
if(index< max)
{
found [index] = start;
string testsubject = ExtractMainSubject(start.subject);
for(int x = 0; x< master.Count; x ++)
{
Header test =(Header)master [x];
if(test.subject.Contains(testsubject))
{
//master.Remove(test);
int testIndex;
int testMax;
ExtractMessageNumber(test.subject,out testIndex,out testMax);
//Console.WriteLine(counter) ;
if(testIndex< max)
{
found [testIndex] = test;
counter ++;
}
}
}
//Console.WriteLine("counter = {0}" ,柜台);
int testmax = max-1;
if(counter == testmax)
{
foundm ++;
for(int t = 1; t< found.Length; t ++)
{
Console.WriteLine("我们有一个匹配{0}",找到[t] .subject);
}
}
}
}
}
private void ExtractMessageNumber(string subject,out int number,out
int max)
{
匹配m = seg.Match(主题);
string segsplit = m.ToString()。替换("(",") ;")。替换(")","");
string [] segments = segsplit.Split(''/'');
number = int.Parse(segments [0]);
max = int.Parse(segments [1]);
}
private string ExtractMainSubject(string subject)
{
返回seg.Replace(subject,"");
}
}
}
I can''t see anything specifically wrong with your code. The RemoveAt(0)
should remove one item each time around the major loop, so the array
should eventually become empty.
I would recommend running your program under the debugger and watch
what it does. It is highly likely that you''ll see the mistake quite
quickly.
Unfortunately I can''t test it, as I don''t have MySql installed.
I did some rearrangement to make it easier to read / debug. Caveat: I
can''t compile this code because I don''t have MySql, so I make no
guarantees that it will compile / run. I also changed your Header
struct to a class. I see no advantage in using "struct" for Header.
Following is the rearranged code.
using System;
using System.Collections;
using System.Text;
using MySql.Data;
using System.Text.RegularExpressions;
namespace createfiles
{
class Program
{
static string[] groups = {
"alt.binaries.games.xbox", "alt.binaries.games.xbox360",
"alt.binaries.vcd" };
static Regex reg = new Regex("\\.");
static Regex seg = new
Regex("\\([0-9]*/[0-9].*\\)",RegexOptions.IgnoreCase);
class Header
{
public string numb;
public string subject;
public string date;
public string from;
public string msg_id;
public string bytes;
}
static void Main(string[] args)
{
for (int x = 2; x < groups.Length; x++)
{
string table = reg.Replace(groups[x], "");
ArrayList master = getheaders(table);
Console.WriteLine("Have this many headers {0}", master.Count);
Header one = (Header)master[0];
Console.WriteLine("first one {0} {1}", one.numb, one.subject);
find(master);
}
}
static ArrayList getheaders(string table)
{
MySql.Data.MySqlClient.MySqlConnection conn
= new MySql.Data.MySqlClient.MySqlConnection();
MySql.Data.MySqlClient.MySqlCommand cmd =
new MySql.Data.MySqlClient.MySqlCommand();
string myConnectionString =
"server=127.0.0.1;uid=root;pwd=password;database=t est;";
conn.ConnectionString = myConnectionString;
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "select * from " + table + " where subject like
''%(%/%)%'' order by subject desc";
MySql.Data.MySqlClient.MySqlDataReader reader;
reader = cmd.ExecuteReader();
Header h = new Header();
ArrayList master = new ArrayList();
while (reader.Read())
{
h.numb = reader.GetValue(0).ToString();
h.subject = reader.GetValue(1).ToString();
h.from = reader.GetValue(2).ToString();
h.date = reader.GetValue(3).ToString();
h.msg_id = reader.GetValue(4).ToString();
h.bytes = reader.GetValue(5).ToString();
master.Add(h);
}
reader.Close();
conn.Close();
return master;
}
static void find(ArrayList master)
{
int foundm = 0;
while (master.Count > 0)
{
Header start = (Header)master[0];
master.RemoveAt(0);
int index;
int max;
ExtractMessageNumber(start.subject, out index, out max);
max += 1;
int counter = 1;
Header[] found = new Header[max];
int temp = master.Count;
if (index < max)
{
found[index] = start;
string testsubject = ExtractMainSubject(start.subject);
for (int x = 0; x < master.Count; x++)
{
Header test = (Header)master[x];
if (test.subject.Contains(testsubject))
{
//master.Remove(test);
int testIndex;
int testMax;
ExtractMessageNumber(test.subject, out testIndex, out testMax);
//Console.WriteLine(counter);
if (testIndex < max)
{
found[testIndex] = test;
counter++;
}
}
}
//Console.WriteLine("counter = {0}", counter);
int testmax = max-1;
if (counter == testmax)
{
foundm++;
for (int t = 1; t < found.Length; t++)
{
Console.WriteLine("We Have a Match {0}", found[t].subject);
}
}
}
}
}
private void ExtractMessageNumber(string subject, out int number, out
int max)
{
Match m = seg.Match(subject);
string segsplit = m.ToString().Replace("(", "").Replace(")", "");
string[] segments = segsplit.Split(''/'');
number = int.Parse(segments[0]);
max = int.Parse(segments[1]);
}
private string ExtractMainSubject(string subject)
{
return seg.Replace(subject, "");
}
}
}
这篇关于Arraylist.remove问题?请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!