开发者

java - check for same values in list

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.pr开发者_开发百科int 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".


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.


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

  • http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html

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

  • http://www.java-tips.org/java-se-tips/java.util/how-to-use-of-hashmap.html

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);
       }
   }
}


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜