Arraylist搜索问题 [英] Arraylist searching question

查看:44
本文介绍了Arraylist搜索问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:


我有一个存储一些非常基本的对象的arraylist。


该对象非常基本,它有2个属性: ID和COUNT


在我向arraylist添加一个对象之前,我想检查arraylist中是否存在具有相同ID的对象
的对象。


如果确实如此,我想将arraylist的
内匹配对象的数量增加1,否则我想添加新对象。


如何查看我的

arraylist中是否已添加该对象?我将如何增加计数?


任何建议和示例代码都会大大贬值!

Hello:

I have an arraylist storing some very basic objects.

The object is very basic, it has 2 properties : ID, and COUNT

Before I add an object to the arraylist, I want to check if an object with
that same ID already exists in the arraylist.

If it does, I would like to increase the count of the matching object inside
of the arraylist by 1, otherwise I want to add the new object.

How would I go about finding out if that object is already added in my
arraylist? And how would I increase the count?

Any advice and sample code would be greatly apreciated!

推荐答案

嗨Colleen,


ArrayList有一个Contains(item)方法,返回一个布尔值

,表示该项是否在列表中。这个方法使用Object.Equals

比较,所以如果你想找到你的对象,它将需要覆盖

那个函数。以下是一个例子:


类simpleObj

公共ID为整数;

public计为整数;

public覆盖函数Equals(obj as simpleObj)as boolean

return obj.ID = ID

end function

End Class


dim myList as New ArrayList

myList.Add(simpleObj1)

if myList.Contains(simpleObj2)then

idx = myList.IndexOf(simpleObj2)

myList(idx).Count + = 1

else

myList.Add (simpleObject2)

结束如果


祝你好运!


- Alex Papadimoulis

C Downey <共************* @ homtail.com>在消息中写道

news:u7 ************** @ TK2MSFTNGP09.phx.gbl ...
Hi Colleen,

The ArrayList has a Contains( item ) method that returns a boolean
indicating whether the item is in the list. this method uses Object.Equals
comparison, so if you want your object to be found, it will need to override
that function. Here''s an example:

Class simpleObj
public ID as integer;
public Count as integer;
public overrides function Equals( obj as simpleObj) as boolean
return obj.ID = ID
end function
End Class

dim myList as New ArrayList
myList.Add(simpleObj1)
if myList.Contains(simpleObj2) then
idx = myList.IndexOf(simpleObj2)
myList(idx).Count += 1
else
myList.Add(simpleObject2)
end if

Good Luck!

-- Alex Papadimoulis
"C Downey" <co*************@homtail.com> wrote in message
news:u7**************@TK2MSFTNGP09.phx.gbl...
你好:

我有一个arraylist存储一些非常基本的对象。

对象是非常基本的,它有2个属性:ID,和COUNT

在我添加之前反对arraylist,我想检查arraylist中是否已经存在具有相同ID的对象。

如果确实如此,我想增加匹配对象的数量
里面的b $ b,否则我想添加新对象。

我怎样才能找出是否已经在我的
arraylist中添加了该对象?我将如何增加计数?

任何建议和示例代码都会非常苛刻!
Hello:

I have an arraylist storing some very basic objects.

The object is very basic, it has 2 properties : ID, and COUNT

Before I add an object to the arraylist, I want to check if an object with
that same ID already exists in the arraylist.

If it does, I would like to increase the count of the matching object inside of the arraylist by 1, otherwise I want to add the new object.

How would I go about finding out if that object is already added in my
arraylist? And how would I increase the count?

Any advice and sample code would be greatly apreciated!



你应该使用Hashtable,它具有更好的搜索性能。


- 布鲁斯(sqlwork.com)

" C Downey" <共************* @ homtail.com>在消息中写道

news:u7 ************** @ TK2MSFTNGP09.phx.gbl ...
you should use a Hashtable, which has much better search performance.

-- bruce (sqlwork.com)
"C Downey" <co*************@homtail.com> wrote in message
news:u7**************@TK2MSFTNGP09.phx.gbl...
你好:

我有一个arraylist存储一些非常基本的对象。

对象是非常基本的,它有2个属性:ID,和COUNT

在我添加之前反对arraylist,我想检查arraylist中是否已经存在具有相同ID的对象。

如果确实如此,我想增加匹配对象的数量
里面的b $ b,否则我想添加新对象。

我怎样才能找出是否已经在我的
arraylist中添加了该对象?我将如何增加计数?

任何建议和示例代码都会大大贬值!
Hello:

I have an arraylist storing some very basic objects.

The object is very basic, it has 2 properties : ID, and COUNT

Before I add an object to the arraylist, I want to check if an object with
that same ID already exists in the arraylist.

If it does, I would like to increase the count of the matching object inside of the arraylist by 1, otherwise I want to add the new object.

How would I go about finding out if that object is already added in my
arraylist? And how would I increase the count?

Any advice and sample code would be greatly apreciated!



谢谢期待你的答复!它是有道理的,而且非常聪明,但是我很难实现它。代码运行但重载的方法

等于总是返回false。

当我在重载的Equals中放置一个断点时代码似乎永远不会
到达那里。

任何想法?

我的代码是:

而博士。阅读

ecMO =新ecMatchingOcc

ecMO.OccID = dr(0)

ecMO.Matches = dr(1)

如果al.Contains(ecMO)那么

idx = al.IndexOf(ecMO)

al(idx).Matches + = 1

Else

al.Add(ecMO)

结束如果

结束时


' ' - =============简单类

公共类ecMatchingOcc''简单类存储匹配职业

个人资料

私有_occID为Int32

私有_匹配为Int32

公共过载功能等于(ByVal obj as ecMatchingOcc)As Boolean

返回obj.OccID = _occID''当我在这里放一个

断点时似乎没有执行

结束功能


公共属性OccID()如Int32

获取

返回_occID

结束获取

设置(ByVal值为在t32)

_occID =价值

结束套件

结束物业


公共物业匹配()作为Int32

获取

返回_匹配

结束获取

设置(ByVal值为Int32)

_matches =价值

结束套件

结束物业

结束等级


" ; Alex Papadimoulis <人*** @ papadimoulis.com>在消息中写道

新闻:Oc ************** @ tk2msftngp13.phx.gbl ...
Thanks for your reply! It makes sense and is very clever however I am
having troubles implementing it. The code runs but the overloaded method
equals always seems to return false.
When I put a breakpoint in the overloaded "Equals" the code never seems to
get there.
Any ideas?
My code is :
While dr.Read
ecMO = New ecMatchingOcc
ecMO.OccID = dr(0)
ecMO.Matches = dr(1)
If al.Contains(ecMO) Then
idx = al.IndexOf(ecMO)
al(idx).Matches += 1
Else
al.Add(ecMO)
End If
End While

'' -- ============= simple class
Public Class ecMatchingOcc '' simple class to store matching Occupational
profiles
Private _occID As Int32
Private _matches As Int32

Public Overloads Function Equals(ByVal obj As ecMatchingOcc) As Boolean
Return obj.OccID = _occID '' doesnt seem to be executed when i put a
breakpoint here
End Function

Public Property OccID() As Int32
Get
Return _occID
End Get
Set(ByVal Value As Int32)
_occID = Value
End Set
End Property

Public Property Matches() As Int32
Get
Return _matches
End Get
Set(ByVal Value As Int32)
_matches = Value
End Set
End Property
End Class

"Alex Papadimoulis" <al***@papadimoulis.com> wrote in message
news:Oc**************@tk2msftngp13.phx.gbl...
嗨Colleen,

ArrayList有一个Contains(item)方法,它返回一个布尔值
,表示该项是否在列表中。这个方法使用Object.Equals
比较,所以如果你想找到你的对象,就需要
覆盖那个函数。以下是一个例子:

类simpleObj
公共ID为整数;
public Count as integer;
public覆盖函数Equals(obj as simpleObj)as boolean
返回obj.ID = ID
结束函数
结束类

将myList作为新的ArrayList调用
myList.Add(simpleObj1)
如果myList.Contains(simpleObj2)然后
idx = myList.IndexOf(simpleObj2)
myList(idx).Count + = 1

myList.Add(simpleObject2)
结束如果

祝你好运!

- Alex Papadimoulis
C Downey <共************* @ homtail.com>在消息中写道
新闻:u7 ************** @ TK2MSFTNGP09.phx.gbl ...
Hi Colleen,

The ArrayList has a Contains( item ) method that returns a boolean
indicating whether the item is in the list. this method uses Object.Equals
comparison, so if you want your object to be found, it will need to override that function. Here''s an example:

Class simpleObj
public ID as integer;
public Count as integer;
public overrides function Equals( obj as simpleObj) as boolean
return obj.ID = ID
end function
End Class

dim myList as New ArrayList
myList.Add(simpleObj1)
if myList.Contains(simpleObj2) then
idx = myList.IndexOf(simpleObj2)
myList(idx).Count += 1
else
myList.Add(simpleObject2)
end if

Good Luck!

-- Alex Papadimoulis
"C Downey" <co*************@homtail.com> wrote in message
news:u7**************@TK2MSFTNGP09.phx.gbl...
你好:

我有一个arraylist存储一些非常基本的对象。

对象是非常基本的,它有2个属性:ID,和COUNT

在我向arraylist添加一个对象之前,我想检查arraylist中是否存在具有相同ID的对象


如果确实如此,我想增加匹配对象的数量
Hello:

I have an arraylist storing some very basic objects.

The object is very basic, it has 2 properties : ID, and COUNT

Before I add an object to the arraylist, I want to check if an object with that same ID already exists in the arraylist.

If it does, I would like to increase the count of the matching object

<在arraylist的


inside

里面加1,否则我想添加新的对象。

我怎样才能找出这个对象是否已添加到我的
arraylist?我将如何增加计数?

任何建议和示例代码都会受到极大的影响!
of the arraylist by 1, otherwise I want to add the new object.

How would I go about finding out if that object is already added in my
arraylist? And how would I increase the count?

Any advice and sample code would be greatly apreciated!




这篇关于Arraylist搜索问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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