1

I have a list with an amount of "turtles" created. This is just some of the code, not all.

int nrTurtles = Keyboard.nextInt("How many turtles to create: ");
w = new GraphicsWindow(500, 300);
for (int k = 1; k <= nrTurtles; k++) {
        Turtle t = new Turtle(w, 50, 50 + 10*k);
            turtles.add(t);
}

And each turtle has a coordination value that i can get with (example):

turtles.get(0).getX() //this gets turtle nr 1 position

Now how do i on a smart way check if two or more turtles depending on how many there are in the list has the same coordination value ( a fix value of 450)?

And if two or more has the same value (450) i want to write with system.out.print that "turtle1 and turtle2 are the ones", if it happens that turtle1 and turtle2 has the same value.

If there is only one turtle having the value of (450) and no other, i want to write only that turtle, for ex. "turtle1 is the only one".

2
  • Does your Turtle class have the equals() method implemented? Commented Nov 25, 2010 at 21:23
  • Can you subclass them/edit the code and add a equals method? Commented Nov 25, 2010 at 21:47

3 Answers 3

1

I'd create a structure like this (pseudocode):

HashTable{
  450 => Array{ 1, 5 },
  300 => Array{ 2 },
  150 => Array{ 3, 4, 6 } 
}

where the key is coordination value, and the value is an array of turtles that have it.

Filling the structure:

for each turtle:
  get coord value
  do we have it as a key yet?
    no: create coord.value => empty array
  insert turtle id into coord.value array

Printing it out is just a matter of iterating over it.

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

1 Comment

(if you only need turtles with coord.value 450, just add a check "get coord value, if not 450, continue")
1

You can use Map<Integer, Set<Turtle>>:

with the key being the X coordinate. Take a look here for how to use HashMaps:

Basically, loop through all the turtles and add it to your map. Something like:

Map<Integer, Set<Turtle>> map = new HashMap<Integer, Set<Turtle>>();
for(Turtle t: turtles) {
   Set<Turtle> set = m.get(t.getX());
   if(set == null) {
      s = new HashSet<Turtle>();
      map.put(t.getX(), set);
   }
   map.put(t.getX(), set);
}

Then you just go through the elements and see if there are more then one:

for(Map.Entry<Integer, Set<Turtle>> e: map.entrySet()) {
   Set<Turtle> set = e.getValue();
   if(set.size() > 1) {
       System.out.println("These turtles have the same X coordinate:");
       for(Turtle t: set) {
          System.out.println(t);
       }
   }
}

Comments

0

The easiest way to do this is just to iterate through the list and compare the relevant values, if they match then add the matching turtles to a results list.

Unless the list of turtles is sorted you're pretty much restricted to iteration as a technique for searching the list.

2nd EDIT:

ArrayList<Turtle> results = new ArrayList<Turtle>();

for (int i = 0; i < nrTurtles; i++) {

    if (turtles.get(i).getX() == 450) {

        results.add(turtles.get(i));
    }
}

if (results.size() == 0) {

    // No turtles are at 450

} else if (results.size() == 1) {

    // Turtle is alone at 450

} else {

    // Turtles are all at 450
}

4 Comments

Yes, that looks good, but just one question. What if the position of the two turtles are 300, then your code would check if turtles with 300 are equal. I wanted only to test if the value was 450. Hope u understand what i mean.
Is this O(n^2)? Also, this doesn't really work for 3 and more turtles "together".
I've edited the answer to show what I think you're looking for. If this is homework though you should really take a good crack at it yourself first. @Piskvor yes, it was O(n^2). I agree that Hashtables are very useful but it didn't seem like that was the sort of thing he was after.
Ahh, that is exactly what i was looking for. Thank you so much! Yes this was a homework and i will definitly look at your example and learn. When i code i don't like to copy and paste, i want to understand ;)

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.