数组列表? [英] ArrayList?

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

问题描述

嘿,

我有一个arraylist,我已经添加了许多对象(所有相同的

类型的对象),我需要能够检查如果数组在添加之前已经包含了

对象,我尝试了类似的东西...


如果不是myArray.contains(newObject)那么

myArray.Add(newObject)

结束如果


这将使用字符串对象但不是用户定义的对象。 />
我可以使用HashTable来处理工作,但我确定在使用ArrayList之前我已经完成了这个



任何人都可以让我知道如果以及如何做到这一点。


所有最好的

Kym

解决方案

"许姆" < ky ** @ iprimus.com.auschrieb


嘿,

我有一个arraylist,我添加了一些对象(所有

相同类型的对象),我需要能够在添加它之前检查数组

是否已经包含该对象,我已经尝试了一些东西

喜欢...


如果不是myArray.contains(newObject)那么

myArray.Add(newObject)

结束如果


这将使用字符串对象而不是用户定义的对象。

我可以使用HashTable让事情变得有效但是我我确定在使用ArrayList之前我已经完成了这个。

任何人都可以让我知道如果和如何做到这一点。



Arraylist.contains也应该与其他对象类型一起使用。如果再次尝试使用
(使用String,Point,Form),它就可以了。你怎么注意到它

不起作用?

Armin


为什么不用它通用名单?类似


dim myList as new List(Of UserDefinedObject)()


如果不是myList.Contains(newObject)那么

myList.Add(newObject)

结束如果


谢谢,


Seth Rowe

2月7日凌晨2:30,Kym < k ... @ iprimus.com.auwrote:


嘿,

我有一个arraylist,我添加了一些对象(所有相同的

类型的对象),我需要能够在添加它之前检查数组是否已经包含

对象,我尝试过像...


如果不是myArray.contains(newObject)那么

myArray.Add(newObject)

结束如果


这将使用字符串对象但不是用户定义的对象。

我可以通过使用HashTable让事情变得有效但是我确定我有在使用ArrayList之前完成了这个



任何人都可以告诉我如果和如何做到这一点。


所有最好的

Kym



在这里尝试几件事。


1)让你的用户定义的对象类实现IComparable所以它

暴露了你的类的用户(如ArrayList)mi的一些方法gbt

能够比较你的对象。


2)让你的用户定义的对象类重写等于(再次为

相同原因)。


================

Clay Burch

Syncfusion ,Inc。


Hey,
I have an arraylist which I have added a number of objects to (all the same
type of object), I need to be able to check if the array already contain the
object before adding it, I have tried something like...

if Not myArray.contains(newObject) then
myArray.Add(newObject)
end if

This will work with a string object but not a user defined object.
I can get things to work by using a HashTable but I''m sure I have done this
before using an ArrayList.
Can anyone let me know If and How to do this.

All the Best
Kym

解决方案

"Kym" <ky**@iprimus.com.auschrieb

Hey,
I have an arraylist which I have added a number of objects to (all
the same type of object), I need to be able to check if the array
already contain the object before adding it, I have tried something
like...

if Not myArray.contains(newObject) then
myArray.Add(newObject)
end if

This will work with a string object but not a user defined object.
I can get things to work by using a HashTable but I''m sure I have
done this before using an ArrayList.
Can anyone let me know If and How to do this.


Arraylist.contains should work with other object types, too. If tried it
again (with String, Point, Form) and it works. How did you notice that it
does not work?
Armin


Why not just use a generic list? Something like

dim myList as new List(Of UserDefinedObject)()

if not myList.Contains(newObject) then
myList.Add(newObject)
end if

Thanks,

Seth Rowe
On Feb 7, 2:30 am, "Kym" <k...@iprimus.com.auwrote:

Hey,
I have an arraylist which I have added a number of objects to (all the same
type of object), I need to be able to check if the array already contain the
object before adding it, I have tried something like...

if Not myArray.contains(newObject) then
myArray.Add(newObject)
end if

This will work with a string object but not a user defined object.
I can get things to work by using a HashTable but I''m sure I have done this
before using an ArrayList.
Can anyone let me know If and How to do this.

All the Best
Kym



A couple of things to try here.

1) have your userdefined object class implement IComparable so it
exposes some means that users of your class (like ArrayList) might be
able to compare your object.

2) have your userdefined object class override Equals (again for the
same reason).

================
Clay Burch
Syncfusion, Inc.


这篇关于数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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