Reputation: 83
I have this ArrayList - ArrayList<Object[]> tree
...
I also have this Array - Object[] move
...that has a size of 2.
In my program a unique 2D array is added to move[0]
and an integer to move[1]
. This array is then added to the ArrayList then the process is repeated so I have a list of 'moves'.
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.
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
. So I would be left with an array/list of 2D arrays that contain the required moves.
Thanks, Matt
Upvotes: 0
Views: 787
Reputation: 54074
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.
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.
Upvotes: 1
Reputation: 1149
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
.
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)
Upvotes: 1