English 中文(简体)
java - 核对清单中的相同价值
原标题:java - check for same values in list
  • 时间:2010-11-25 21:16:24
  •  标签:
  • java
  • list

我有一份清单,列出了一定数量的“海龟”。 这只是一些法典,并非全部。

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

现在,如果两个或两个以上海龟,如何以智能方式行事。 取决于名单上有多少份?

<>And/strong>,如果两个或两个以上具有相同价值(450) i 想用system.out.print写“turtle1 and turtle2 are the,” ,如果出现这样的情况,就具有同样的价值。

www.un.org/Depts/DGACM/index_spanish.htm 如果只有一个带(450)和值(0>)的海龟,则只想写这一海龟,因为“海龟1是唯一的”。

最佳回答

这样做的最容易的方法,就是通过清单加以更新,比较相关价值,如果它们与结果清单相匹配。

Unless the list of turtles is disttyed to iteration as an Techno for searching the list.

第2版:

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
}
问题回答

我建立了这样的结构(旧编码):

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

关键在于协调价值,其价值是具有协调价值的一系列微妙因素。

填补结构:

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

印制文件只是一个重复的问题。

您可使用<代码>Map<Integer, Set<Turtle>>:

关键在于X协调。 探讨如何利用哈希姆普:

基本上,穿透所有微妙之处,并添加到你的地图上。 类似:

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

之后,你刚刚走过这些内容,看看是否有更多的东西:

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...