0

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

2
  • What have tried ? Also, IMO, it will be better if you use an object, e.g. Point, instead of a 2d array (Object[]). Commented Apr 22, 2012 at 20:02
  • 1
    I recommend to change your design, instead of ArrayList<Object[]> use a Map<String, Object[]> and have a String constant MOVES to identify your 2D array Commented Apr 22, 2012 at 20:02

2 Answers 2

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.
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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.