快速查找ArrayList中的重复项 [英] Quickly finding duplicates in an ArrayList

查看:143
本文介绍了快速查找ArrayList中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个字符串ArrayList;我需要一个快速的方法来搜索和比较ArrayList的元素和

返回一个包含3个重复项的ArrayList。

例如,如果ArrayList具有以下元素:


elems [0] =" blue"

elems [1] = red

elems [2] =" blue"

elems [3] =" green"

elems [4] =" red"

elems [5] =" red"


我希望有一个功能通过列表,然后返回一个

新的arraylist如:


newElems [0] =" red"

newElems [1] =" red"

newElems [2] =" red"


实际上我的arraylist是一个对象数组,有一个字符串

属性和我的引用如:elems [0] .filename =" red" ;;但上面的例子更容易理解




我希望能够尽快执行此搜索。我个人b $ b个人没有任何真正的经验或熟悉主题

如二叉树等;那样的话会是什么?

需要什么?谢谢。

Basically I have an ArrayList of strings; I need a fast way of
searching through and comparing the elements of the ArrayList and the
return an ArrayList of items that have 3 Duplicates.

For example, if the ArrayList has the following elements:

elems[0] = "blue"
elems[1] = "red"
elems[2] = "blue"
elems[3] = "green"
elems[4] = "red"
elems[5] = "red"

I want to have a function that goes through the list and then returns a
new arraylist like:

newElems[0] = "red"
newElems[1] = "red"
newElems[2] = "red"

In actuality my arraylist is an array of objects, which have a string
property and I''m referencing like: elems[0].filename = "red"; but the
above example is easier to follow.

I would like to be able to perform this search as fast as possible. I
personally don''t have any real experience or familiarity with topics
such as binary trees, etc; would something like that be the route to
take? Thanks.

推荐答案

我知道你可以遵循的两条路线。


首先 - 查看ArrayList.IndexOf方法,这可能是在底层数组中查找某个项目的最快方法。我相信那里

是一个重载来指定开始搜索的索引。


其次,如果你自己创建了arraylist(不是其他的第三个 - 派对

代码)你可以在填充列表时使用ArrayList.Contains()来确保重复项不会首先进入列表。


我的两个欧元美分。 :)


" paradox" < de*@demiurgeinc.com> schrieb im Newsbeitrag

新闻:11 ********************** @ o13g2000cwo.googlegr oups.com ...
I know of two routes that you can follow.

The first - look at the ArrayList.IndexOf method, which is probably the
quickest way to find a certain item in the underlying array. I believe there
is an overload to specify at which index to start the search.

Secondly, if you create the arraylist yourself (not some other 3rd-party
code) you could use ArrayList.Contains() when populating the list to ensure
the duplicates don''t get into the list in the first place.

My "two" euro cents. :)

"paradox" <de*@demiurgeinc.com> schrieb im Newsbeitrag
news:11**********************@o13g2000cwo.googlegr oups.com...
基本上我有一个字符串ArrayList;我需要一种快速的方法来搜索和比较ArrayList的元素,并返回一个包含3个重复项的ArrayList。

例如,如果ArrayList有以下要素:

elems [0] =" blue"
elems [1] =" red"
elems [2] =" blue"
elems [3] =" green"
elems [4] =" red"
elems [5] =" red"

我想要一个功能通过列表,然后返回一个新的arraylist,如:

newElems [0] =" red"
newElems [1] =" red"
newElems [2] =" red"

实际上我的arraylist是一个对象数组,它有一个字符串
属性,我正在引用如:elems [0]。 filename =" red" ;;但上面的例子更容易理解。

我希望能够尽快执行此搜索。我个人没有任何真正的经验或对二元树等主题的熟悉程度;会是这样的途径吗?谢谢。
Basically I have an ArrayList of strings; I need a fast way of
searching through and comparing the elements of the ArrayList and the
return an ArrayList of items that have 3 Duplicates.

For example, if the ArrayList has the following elements:

elems[0] = "blue"
elems[1] = "red"
elems[2] = "blue"
elems[3] = "green"
elems[4] = "red"
elems[5] = "red"

I want to have a function that goes through the list and then returns a
new arraylist like:

newElems[0] = "red"
newElems[1] = "red"
newElems[2] = "red"

In actuality my arraylist is an array of objects, which have a string
property and I''m referencing like: elems[0].filename = "red"; but the
above example is easier to follow.

I would like to be able to perform this search as fast as possible. I
personally don''t have any real experience or familiarity with topics
such as binary trees, etc; would something like that be the route to
take? Thanks.



虽然它是用VB.NET编写的,但下面链接中的演示项目很容易跟随和接下来应该会有所帮助。

http: //getdotnetco.web101.discountas...wnloadPage.htm

" paradox" < de*@demiurgeinc.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
Though it is written in VB.NET, the demo project at the link below is easy
to follow and should be helpful.

http://getdotnetco.web101.discountas...wnloadPage.htm
"paradox" <de*@demiurgeinc.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
基本上我有一个字符串ArrayList;我需要一种快速的方法来搜索和比较ArrayList的元素,并返回一个包含3个重复项的ArrayList。

例如,如果ArrayList有以下要素:

elems [0] =" blue"
elems [1] =" red"
elems [2] =" blue"
elems [3] =" green"
elems [4] =" red"
elems [5] =" red"

我想要一个功能通过列表,然后返回一个新的arraylist,如:

newElems [0] =" red"
newElems [1] =" red"
newElems [2] =" red"

实际上我的arraylist是一个对象数组,它有一个字符串
属性,我正在引用如:elems [0]。 filename =" red" ;;但上面的例子更容易理解。

我希望能够尽快执行此搜索。我个人没有任何真正的经验或对二元树等主题的熟悉程度;会是这样的途径吗?谢谢。
Basically I have an ArrayList of strings; I need a fast way of
searching through and comparing the elements of the ArrayList and the
return an ArrayList of items that have 3 Duplicates.

For example, if the ArrayList has the following elements:

elems[0] = "blue"
elems[1] = "red"
elems[2] = "blue"
elems[3] = "green"
elems[4] = "red"
elems[5] = "red"

I want to have a function that goes through the list and then returns a
new arraylist like:

newElems[0] = "red"
newElems[1] = "red"
newElems[2] = "red"

In actuality my arraylist is an array of objects, which have a string
property and I''m referencing like: elems[0].filename = "red"; but the
above example is easier to follow.

I would like to be able to perform this search as fast as possible. I
personally don''t have any real experience or familiarity with topics
such as binary trees, etc; would something like that be the route to
take? Thanks.



嗯......实际上,代码的重点是找到重复项,然后

然后将它们显示给用户。为了让您更好地理解,我正在创建的程序用于确定Quake 3中使用的pk3(zip)

文件中的冲突。当人们开发新地图时,他们创建并使用现有的地图元素(纹理,着色器等),并将它们全部存储在这些pk3文件中。
。出现的问题是,他们创建了某些着色器或纹理的自有版本,而无需重命名或更改文件名目录结构。这导致游戏无法正确加载
。具体来说,这个问题似乎与

的情况有关,当有3个pk3文件,其中着色器元素具有相同名称但文件大小不同的




我已经浏览了pk3(zip)文件并填充了一个名为

conlficts的arraylist,其中包含一个element类型的对象(我已经定义了一个类)。 br />

我正在尝试提供一份报告,该报告将通过此列表返回

这些元素(及其相关的pk3文件)。这样最终用户可以轻松确定需要删除哪些文件才能成功连接。


使用IndexOf与缓慢的3嵌套循环方法没什么不同

我试图避免。

Well... actually the point of the code is to find the duplicates and
then display them to users. To give you a much better understanding,
the program I am creating is for determining conflicts in the pk3 (zip)
files used in Quake 3. As people develop new maps, they create and use
existing map elements (textures, shaders, etc) and they are all stored
in these pk3 files. The problem that occurs is that they create their
own version of certain shaders or textures without renaming or changing
the filename directory structure; and this causes the game to fail to
load properly. Specifically the problem seems to be associated to
occasions when there are 3 pk3 files with a shader element that has the
same name but different file size.

I''ve gone through the pk3 (zip) files and populated an arraylist called
conlficts with a object of type element (a class I''ve defined).

I''m trying to provide a report that will go thru this list and return
these elements (and their associated pk3 files). So that the end user
can easily determine which files that they need to remove in order to
be able to connect successfully.

Using IndexOf isn''t much different than the slow 3 nested loop approach
I''m trying to avoid.


这篇关于快速查找ArrayList中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆