[英]Checking if an Array element within an ArrayList contains a certain value
I have this ArrayList - ArrayList<Object[]> tree
... 我有这个ArrayList -
ArrayList<Object[]> tree
...
I also have this Array - Object[] move
...that has a size of 2. 我也有这个Array -
Object[] move
...,大小为2。
In my program a unique 2D array is added to move[0]
and an integer to move[1]
. 在我的程序中,添加了一个独特的 2D数组来
move[0]
和一个move[1]
整数move[1]
。 This array is then added to the ArrayList then the process is repeated so I have a list of 'moves'. 然后将此数组添加到ArrayList,然后重复该过程,因此我有一个“移动”列表。
My problem is I am unsure how to find the arrays ('moves') within the ArrayList ('tree') that contain a certain value in the move[1]
element only - as the move[0]
element will be unique everytime. 我的问题是,我不知道该如何找到在含有一定价值的ArrayList(“树”)中的阵列(“行动”)
move[1]
唯一的元素-作为move[0]
元素将是唯一的每次。
I then want to make an array/list of all of the matches. 然后我想制作所有比赛的数组/列表。 For example, an array that contains all of the
move[0]
values that match up to the move[1]
value of 3
. 例如,一个数组,其中包含与
move[1]
值3
匹配的所有move[0]
值。 So I would be left with an array/list of 2D arrays that contain the required moves. 所以我将留下包含所需移动的2D阵列的数组/列表。
Thanks, Matt 谢谢,马特
The simplest way is just iterating through your tree
object and building a new ArrayList
with the found matches, but this will take time linear to the size of that tree
. 最简单的方法是迭代您的
tree
对象并使用找到的匹配构建一个新的ArrayList
,但这将花费时间线性到该tree
的大小。 If you're looking for speed, then you could maintain a HashMap<Integer, ArrayList<Object[]>>
, where the keys are the integers you store in move[1]
, and the value is a list of all the moves that have that key, so retrieving that list can be done in O(1) 如果你正在寻找速度,那么你可以维护一个
HashMap<Integer, ArrayList<Object[]>>
,其中键是你在move[1]
存储的整数,并且值是所有移动的列表有那个键,所以检索该列表可以在O(1)中完成
It seems to me that what you need is a key-value
data structure and you have chosen a rather odd way to implement it. 在我看来,你需要的是一个
key-value
数据结构,你选择了一种相当奇怪的方式来实现它。
You should use either a HashTable
if you can not use generics (you are in an old jdk for some reason) or use a HashMap
using types to enforce type safety. 如果你不能使用泛型(你出于某种原因在旧的jdk中)或者使用
HashMap
使用类型来强制类型安全,你应该使用HashTable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.