Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are probably many ways that you could go about this. Most of which are not known to me. However I have recently solved a similar problem in two different ways:</p> <h1>Two Dimensional Array</h1> <p>Implementing the "Map" as a two Dimensional Array is the obvious solution. Most likely for this scenario you will want an <code>List</code> implementation so that you can resize easily. It will however require nested loops and such to search for a particular location, and the indexes might become somewhat confusing.</p> <h1>Map</h1> <p>A (in my opinion) better way to solve this problem would be to use a <code>Map</code>. You should create a <code>Position</code> class that has two variables (or three for a three-dimensional map) that record the X and Y position (Make sure you also implement the <code>equals()</code> and <code>hashCode()</code> methods). You can then use a <code>Position</code> object as the key in a <code>Map</code> implementation, stored against instances of <code>Animal</code>. This will allow for faster searching of a particular location (see <a href="https://stackoverflow.com/questions/1518103/hashmap-vs-arraylist-performance-am-i-correct">HashMap vs ArrayList performance am I correct</a>), and tidier code, ie no nested loops when iterating over the whole map. You can loop over the whole map like this:</p> <pre><code>for(Map.Entry&lt;Position, Animal&gt; entry : animalMap.getEntries()){ //Do stuff } </code></pre> <p>A couple of years ago I saw a tutorial project that implemented a Fox/Rabbit simulation. You may want to have a look at that for some ideas: <a href="http://www.bluej.org/objects-first/" rel="nofollow noreferrer">http://www.bluej.org/objects-first/</a></p> <p>You need to download the "Book Projects" .zip file. The relevant project is in Chapter 10.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload